<?php //copyright
require_once('./fun.php');
require_once('./hd_fun.php');
$conn=my_sql($cfg);
$appid=g('appid');

if($cfg['debug']==1){//开启debug

}else{
$redis=redis();
}

check_sign();

$err['err']='ok';
$ac=g('ac');
if(!$ac){exit;}
$set=get_set();
$id=g('id');if(!$id or !is_numeric($id)){$id=0;}


switch($ac){

case 'get_hd_detail'://获取活动
if(!$id){err('请指定活动');}
$c=g('c');//是否进行reload
if($C!='reload'){$c='load';}
$user=is_login();
$err['set']=$set;
$err['user']=$user;
$err['can_edit']=0;//是否显示编辑标志


$sql='select * from '.$cfg['e'].'hd where id="'.a($id).'" ';
$l=db1($sql);if(!$l){err('没有找到活动，或者该活动已经删除');}

if($user['is_emp']==$l['shop_id'] and $user['id']==$l['user_id'] and $user['emp_state']>0){$err['can_edit']=1;}
if($l['shop_id']==$user['shop_id'] and $user['shop_state']>0){$err['can_edit']=1;}

$err['can_buy']=0;//是否可以购买
$shop=get_hd_shop($l['shop_id']);
$err['shop']=$shop;

if($l['end']){$l['end_time']=date('Y年m月d日 H:i',$l['end']);}

if($l['title']){$l['title']=base64($l['title']);}
if($l['info']){$l['info']=base64($l['info']);}
if($l['html']){$l['html']=unserialize(base64($l['html']));}
if($l['tag']){$l['tag']=unserialize(out($l['tag']));

if($l['nuo_data']){$l['nuo_data']=base64($l['nuo_data']);}
if($l['fen_data']){
$l['fen_data']=unserialize(base64($l['fen_data']));
}


if(is_array($l['tag'])){
$err['info']['tag_num']=count($l['tag']);
}
}else{$l['tag']=array();}

if($l['img']){$l['img']=get_imgs($l['img']);}

$err['reload']=$set['reload'];//毫秒
if(!$err['reload']){$err['reload']=30000;}


if($l['state']==1 or $err['can_edit']==1){
if($l['start']>time()){
$l['hd_state']='距开始还剩:';
$err['time']=$l['start'];
$l['is_start']=1;
}else{
if($l['end']<time()){
$l['hd_state']='抱歉，活动已经结束!';$err['reload']=0;
}else{
$l['hd_state']='活动倒计时:';
if($l['state']==1){$err['can_buy']=1;}
$err['time']=$l['end'];
}
}

}else{
$l['hd_state']='抱歉，该活动还未上线，敬请关注！';$err['reload']=0;
}


//活动商品

if($l['pro']){$pro=unserialize(out($l['pro']));//活动商品
$l['pro']=$pro;
$hd=$l;
$title = g('title');
if($title){$err['pro']=get_pros($pro,$l['zk'],1,$title);}else{
    $err['pro']=get_pros($pro,$l['zk'],1);
}
}

//红包显示
if($l['hb_price']>0 and $l['hb_num'] and $l['hb_total']){
if($l['hb_price']>=1){$l['hb_price']=number_format($l['hb_price'],0,'.','');$l['hb_daiwei']='元';}else{
$l['hb_daiwei']='毛';
$l['hb_price']=number_format($l['hb_price']*10,0,'.','');
}


if($user){//如果已经登录，则计算获取的红包数
if($redis){//我邀请的人数
$hb_num=$redis->get('hd_'.$l['id'].'_get_'.$user['id']);
}else{
$sql='select count(id) as num from '.$cfg['e'].'hb_user where user_id="'.$user['id'].'" and hd_id="'.$l['id'].'"';
$hd_user_num=db1($sql);
if($hd_user_num){
$hb_num=$hd_user_num['num'];
}
}

//查找我是否已经获取的红包
$l['my_hb_get_user']=$hb_num;
if($hb_num>=$l['hb_num']){//如果超过需要的数量，查看是否已经获取了红包
$sql='select id from '.$cfg['e'].'hb_pay where user_id="'.$user['id'].'" and hd_id="'.$l['id'].'" and state>0';
$x=db1($sql);
if($x){$l['my_hb_get_user']=0;}
}

}
}


$err['list']=$l;

if($redis){
$hd_cache=$redis->get('hdcache_'.$l['id']);
if($hd_cache){$hd_cache=unserialize(out($hd_cache));}
if(!$hd_cache or !is_array($hd_cache)){$hd_cache=[];}
//print_r($hd_cache);
}

//秒杀商品
if($l['miao_pro'] and $l['miao_price']>0){
$miao['pro']=get_miao_pro($l['miao_pro']);
//查找秒杀数量
$cache_key='miao_'.$l['miao_pro'];
$miao_total=$l['miao_total'];
$miao_num=0;
if($miao_total>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $k=>$v){
if($v['date']<time()){unset($hd_cache[$cache_key][$k]);$hd_cache_save++;}else{
$miao_num++;
}
}
$miao_total=$miao_total-$miao_num;
if($miao_total<0){$miao_total=0;}
}}


$miao['title']=$l['miao_title'];
$miao['price']=$l['miao_price'];
$miao['total']=$miao_total;
$l['miao_total']=$miao_total;



if($l['order_num']>0 and $user){//是否已经购买秒杀了

$sql='select id from '.$cfg['e'].'order_pro where c="秒杀" and user_id="'.$user['id'].'" and  hd_id="'.$l['id'].'" and state>0 ';
$has_miao=db1($sql);
if($has_miao){
$miao['total']=0;//已经秒杀了
}
}
$err['miao']=$miao;
}

if($l['mz_pro'] and $l['mz_price']>0){
$mz['title']=$l['mz_title'];
$mz['price']=$l['mz_price'];
$mz['pro']=get_mz_pro($l['mz_pro']);
//查找秒杀数量
$cache_key='mz_'.$l['mz_pro'];
$mz_total=$l['mz_total'];
$mz_num=0;
if($mz_total>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $k=>$v){
if($v['date']<time()){unset($hd_cache[$cache_key][$k]);$hd_cache_save++;}else{
$mz_num++;
}
}
$mz_total=$mz_total-$mz_num;
if($mz_total<0){$mz_total=0;}
}}

$l['mz_total']=$mz_total;
$mz['total']=$mz_total;
if($mz['pro']){$err['mz']=$mz;}
}

if($hd_cache_save and $redis){
$time_end=$l['end']-time();if($time_end<0){$time_end=3600;}
r_set('hdcache_'.$l['id'],$hd_cache,$time_end);
}


//$err['share'][]=array('title'=>'朋友圈','url'=>'https://img.'.$cfg['domain'].'/quan.png');
//$err['share'][]=array('title'=>'分享群','url'=>'https://img.'.$cfg['domain'].'/share.png');


//获取活动订单
$err['order']=get_hd_order($l,1);

//判断是否有虚拟销量如果有增加销量
if($l['virtual_people']){
    // 确保 $err['order'] 是数组（若未初始化则设为空数组）
    $err['order'] = is_array($err['order']) ? $err['order'] : [];
    $err['order'] = array_merge($err['order'], virtual_people($l,$err['pro']));
}

if($err['order']){$err['order_num']=count($err['order']);}


if($c=='load'){
//我的订单数
$sql='select count(id) as num from '.$cfg['e'].'order where shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'" and state=1 ';
$my_order_num=db1($sql);
if($my_order_num){$err['my_order_num']=$my_order_num['num'];}
//优惠券
$quan=get_hd_quan($l['id']);
$err['my_quan']=get_hd_my_quan($l['id']);//我的优惠券
if($err['my_quan']){//如果存在我的优惠券
foreach($err['my_quan'] as $k=>$v){
if(is_array($quan)){unset($quan['item_'.$v['quan_id']]);}//删除掉优惠券
if($v['is_use'] or ($v['end']<time() and $v['end'])){unset($err['my_quan'][$k]);}//删除优惠券
}
}
if(!$quan){$quan='';}
$err['quan']=$quan;
if($quan){$err['quan_show']='show';
$err['info']['quan_num']=count($quan);
//$err['info']['quan_bottom']='点击领取优惠券';
//$err['info']['quan_bg']=['https://img8.zjhn.com/quan/hb_bg.png','https://img8.zjhn.com/quan/hb_bg1.png','https://img8.zjhn.com/quan/hb_item.png'];//优惠券底部 
}


//消息
$err['msg']=get_hd_msg($l['id']);



if($user){//是否关注了门店
$sql='select * from '.$cfg['e'].'shop_gz where  shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'" ';
$is_gz=db1($sql);
if(!$is_gz){
$err['need_gz']=1;
$arr['shop_id']=$shop['id'];
$arr['user_id']=$user['id'];
$arr['shop_name']=$shop['shop_name'];
$arr['date']=time();
$arr['from_id']=g('from_id');if(!$arr['from_id']){$arr['from_id']=0;}
$arr['logo']=$user['logo'];
insert($cfg['e'].'shop_gz',$arr);

//门店关注人数
$sql='update '.$cfg['e'].'shop set people_num=people_num+1 where id="'.$shop['id'].'"';
sql_query($sql);
$sql='update '.$cfg['e'].'users set shop_gz_num=shop_gz_num+1 where id="'.$user['id'].'"';
sql_query($sql);

}else{
if(!$is_gz['need_post']){$err['need_gz']=1;}//是否接受推送
}
}

//$hd_arr['view']=$l['view']+1;
//update($cfg['e'].'hd',$hd_arr,' where id="'.$l['id'].'"');
$sql='update '.$cfg['e'].'hd set view=view+1 where id="'.$l['id'].'"';
sql_query($sql);

}

$sql='select * from '.$cfg['e'].'yun where id="'.$l['yun_id'].'"';
$yun=db1($sql);
$err['yun']=$yun;

//查看是否需要更新动态
//if($l['activity_id']){
//if($l['activity_out']<time()){
// //get_activity_id($l);}else{
//update_activity_msg($l);}
//}


echo json_encode($err);


break;

case 'get_more_order'://获取活动更多的订单
$id=g('id');if(!$id){err('ok');}
$page=g('page');if(!$page){$page=1;}
$sql='select * from '.$cfg['e'].'hd where id=:id ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('ok');}
$err['order']=get_hd_order($l,$page);
if($err['order']){$err['order_num']=count($err['order']);}
echo json_encode($err);
break;

case 'get_hd_list'://获取活动列表
$user=is_login();
$err['user']=$user;
$err['set']=$set;
//if(!$user){err('no_login');}
$show_shop=g('show_shop');

if($show_shop==1){//显示门店
$id=g('shop_id');
if($user and !$id){
$id=$user['shop_id'];
if($id){$id=$user['is_emp'];}
}

$err['shop']=get_hd_shop($id);
$err['gz']=get_gz($id);

if($user){
$re=get_gz_list($id);    
$need_post=$re['item'];
}

}else{

if($user){//获取关注的门店

$re=get_gz_list($id);    
$need_post=$re['item'];
$ids=$re['shop'];
if($ids){
foreach($ids as $v){
if($v){
if($id){$id.=','.$v;}else{$id=$v;}
}
}
    
//$id=implode(',',$ids);

}
//}


if(!$id){$id=0;}

if($user['shop_id']){
$id.=','.$user['shop_id'];
}else{
if($user['is_emp']){
$id.=','.$user['is_emp'];
}
}

}
}

if(!$id){//如果没有关注点
$id=$set['shop_id'];//默认门店
if(!$id){$id=2;}
}

$err['need_post']=$need_post;


//$sql='select * from '.$cfg['e'].'hd where  state=1  and shop_id in ('.$id.') ';

$sql='select id,title,img,date,start,end,shop_name,people_num,order_num,min,max,logo,view,shop_id,show_name,miao_price,miao_pro from '.$cfg['e'].'hd where  state=1  and shop_id in ('.$id.',0) ';

$key=g('key');
$page_num=200;
if($key){
$sql.=' and temp like  "%'.a($key).'%" ';
$page_num=100;
}

$sql.=' order by id desc limit '.$page_num;

$l=db($sql,$p);
if($l){
foreach($l as $v){
$v['img']=get_imgs($v['img']);
$v['img_num']=count($v['img']);
$v['title']=base64($v['title']);
//$v['info']=base64($v['info']);
$v['shop_name']=base64($v['shop_name']);
if($v['people_num']>0){
$v['order']=get_hd_order($v,1,0);
}

$v['up_time']=date_ff($v['date']);
if($v['start']>time()){//还没开始
$v['time']='开始时间:'.date('m月d日 H:i',$v['start']);
$v['style']='gay_text';

$start[]=$v;
}else{
if($v['end']<time()){//结束
$v['time']='团购已结束';
$v['time_text']=$v['time'];
$v['style']='gay_text';
$end[]=$v;
}else{//进行中
$v['time']='正在跟团中';
$v['time_text']='截止时间:'.date('m月d日 H:i',$v['end']);
$open[]=$v;

}
}
}

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=10;$s=($page-1)*$page_num;
if($start){foreach($start as $v){$open[]=$v;}}
if($end){foreach($end as $v){$open[]=$v;}}

if($open){
$re=array_slice($open,$s,$page_num);
}




$err['list']=$re;
$err['num']=count($re);
}

//订单

$err['order_num']=get_my_order_num('1,2,5');
if($err['order_num']>100){$err['order_num']=99;}

echo json_encode($err);

break;

case 'cart_submit'://提交活动订单
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');if(!$id){err('请指定活动');}

$data=p('data');if(!$data){err('购物车商品为空');}
$data=json_decode($data,1);if(!$data){err('购物车数据错误，如有问题，请联系管理员');}
if(!$data['pro']){err('您的购物车中没有商品，请添加商品后继续！');}

$sql='select * from '.$cfg['e'].'hd where id=:id  ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动');}
if($l['state']!=1){err('抱歉，该活动还没有开始');}
if($l['end']<time()){err('抱歉，活动已经结束了');}
if($l['start']>time()){err('很抱歉，活动还没有开始。');}

if($redis){
$hd_cache=$redis->get('hdcache_'.$l['id']);
if($hd_cache){$hd_cache=unserialize(out($hd_cache));}
if(!$hd_cache or !is_array($hd_cache)){$hd_cache=[];}
//print_r($hd_cache);
}

$shop=get_hd_shop($l['shop_id']);

if($l['pro']){$pro=unserialize(out($l['pro']));//活动商品
$hd=$l;
$pros=get_pros($pro,$l['zk']);
//print_r($pro);
//exit;
}
$pro_price=0;$num=0;
$index=0;
//秒杀
if($data['miao'] and $l['miao_price']>0){//存在秒杀
$pro=get_miao_pro($l['miao_pro']);

$cache_key='miao_'.$l['miao_pro'];$mz_total=$l['miao_total'];$mz_num=0;
if($mz_total>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $k=>$v){
if($v['date']<time()){unset($hd_cache[$cache_key][$k]);$hd_cache_save++;}else{
$mz_num++;
}
}
$mz_total=$mz_total-$mz_num;
if($mz_total<0){$mz_total=0;}
}}
$l['miao_total']=$mz_total;

if($l['miao_total']==0){err('抱歉，秒杀商品已经抢光了，请重新下单。');}

$cart[$index]['title']=base64_encode($pro['title']);
if($l['miao_title']){$cart[$index]['title']=$l['miao_title'];}

$cart[$index]['temp_title']=$pro['title'];
$cart[$index]['img']=$pro['img'];
$cart[$index]['price']=$l['miao_price'];
$cart[$index]['num']=1;
$cart[$index]['c']='秒杀';
$cart[$index]['date']=time();
$cart[$index]['shop_id']=$l['shop_id'];
$cart[$index]['pro_id']=$pro['id'];
$cart[$index]['user_id']=$user['id'];
$cart[$index]['state']=0;
$cart[$index]['pro_price']=$pro['price'];
$cart[$index]['total']=$l['miao_price'];


if($l['miao_total']>0){$cart[$index]['cache_key']='miao_'.$pro['id'];}//如果有数量，则缓存key

$pro_price+=$l['miao_price'];$num++;
}

//商品


foreach($data['pro'] as $k=>$v){//item_id
$pro=$pros[$k];$index++;
if($v['data']){
foreach($v['data'] as $j=>$t){//c
foreach($t as $m=>$n){
$index++;
$i=str_replace('item_','',$m);
if($i=='' and !is_numeric($i)){//没有属性
$d=$pro['data'][$j];

}else{
$d=$pro['data'][$j]['item'][$i];
}

if($d['img']){
$cart[$index]['img']=$d['img'];
}else{
$cart[$index]['img']=$pro['imgs'][0]['img_240'];
}


$price=$d['price'];if(!$price){$price=$pro['price'];}
$max=$d['num'];//总数量

$buy=$n['buy'];//购买数量

if($max==0){$err_msg['is_empty']++;//库存不够
}else{
if($max>0 and $buy>$max){$buy=$max;$err_msg['is_not_enough']++;}

$cart[$index]['title']=base64_encode($pro['title']);
$cart[$index]['temp_title']=$pro['title'];
$cart[$index]['num']=$buy;
$cart[$index]['c']='pro';
$cart[$index]['date']=time();
$cart[$index]['shop_id']=$l['shop_id'];
$cart[$index]['pro_id']=$pro['id'];
$cart[$index]['user_id']=$user['id'];
$cart[$index]['state']=0;
$cart[$index]['price']=$price;
$cart[$index]['pro_price']=$pro['price'];//商品的原理价格
$cart[$index]['total']=number_format($price*$buy,2,'.','');
$cart[$index]['info']=$n['info'];
$cart[$index]['data']=serialize($t);
if($max>0){$cart[$index]['cache_key']='pro_'.$pro['id'].'_'.$j.'_'.$i;}//商品缓存key

$pro_price+=$cart[$index]['total'];$num+=$buy;
}


}
}
}else{
if($pro['total']==0){//商品已经售罄
$err_msg['is_empty']++;
}else{
 $buy=$v['buy'];   

if($pro['total']>0 and $buy>$pro['total']){$buy=$pro['total'];$err_msg['is_not_enough']++;}
$cart[$index]['title']=base64_encode($pro['title']);
$cart[$index]['img']=$pro['imgs'][0]['img_240'];
$cart[$index]['price']=$pro['price'];
$cart[$index]['num']=$buy;
$cart[$index]['c']='pro';
$cart[$index]['date']=time();
$cart[$index]['shop_id']=$l['shop_id'];
$cart[$index]['pro_id']=$pro['id'];
$cart[$index]['user_id']=$user['id'];
$cart[$index]['state']=0;
$cart[$index]['pro_price']=$pro['price'];
$cart[$index]['total']=number_format($pro['price']*$buy,2,'.','');
if($pro['total']>0){$cart[$index]['cache_key']='pro_'.$pro['id'];}//商品缓存key
$pro_price+=$cart[$index]['total'];$num+=$buy;
}
}

}

//赠品
if($l['mz_pro'] and $pro_price>=$l['mz_price'] and $num>0){$index++;
$cache_key='mz_'.$l['mz_pro'];$mz_total=$l['mz_total'];$mz_num=0;
if($mz_total>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $k=>$v){
if($v['date']<time()){unset($hd_cache[$cache_key][$k]);$hd_cache_save++;}else{
$mz_num++;
}
}
$mz_total=$mz_total-$mz_num;
if($mz_total<0){$mz_total=0;}
}}
$l['mz_total']=$mz_total;

if($l['mz_total']!=0){

$pro=get_mz_pro($l['mz_pro']);
$arr['man_id']=$pro['id'];
$cart[$index]['title']=base64_encode($pro['title']);
if($l['mz_title']){$cart[$index]['title']=$l['mz_title'];}
$cart[$index]['img']=$pro['img'];
$cart[$index]['price']=0;
$cart[$index]['num']=1;
$cart[$index]['c']='赠品';
$cart[$index]['date']=time();
$cart[$index]['shop_id']=$l['shop_id'];
$cart[$index]['pro_id']=$pro['id'];
$cart[$index]['user_id']=$user['id'];
$cart[$index]['state']=0;
$cart[$index]['pro_price']=0;
$cart[$index]['total']=0;
$cart[$index]['cache_key']='mz_'.$pro['id'];
}
}

if($hd_cache_save and $redis){
$time_end=$l['end']-time();if($time_end<0){$time_end=3600;}
r_set('hdcache_'.$l['id'],$hd_cache,$time_end);
}


//优惠券
$sql='select * from '.$cfg['e'].'quan_get where  user_id="'.$user['id'].'" and hd_id="'.$l['id'].'" and state=1 order by man asc';
$quan=db($sql);
$quan_price=0;
if($quan){
foreach($quan as $v){
if(($v['end']>0 and $v['end']<time()) or $v['is_use']){
$quan_arr['state']=2;update($cfg['e'].'quan_get',' where id="'.$v['id'].'"');
}else{
if($pro_price>=$v['man']){
$quan_price=$v['price'];
$arr['quan_id']=$v['id'];
$arr['quan_price']=$v['price'];
$arr['quan_info']='满'.$v['man'].'减'.$v['price'].'元';
}
}
}
}

//运费暂时不进行设置
$arr['hd_id']=$l['id'];
$arr['shop_id']=$l['shop_id'];


$arr['shop_name']=base64_encode($shop['shop_name']);
$arr['shop_addr']=$shop['addr'];
$arr['user_id']=$user['id'];
$arr['nickname']=base64_encode($user['nickname']);
$arr['logo']=$user['logo'];
$arr['date']=time();
$arr['name']=$user['name'];
$arr['phone']=$user['phone'];
$arr['pro']=$user['pro'];
$arr['city']=$user['city'];
$arr['dist']=$user['dist'];
$arr['addr']=$user['addr'];
$arr['data']=serialize($data);$arr['state']=0;
$arr['num']=$num;$arr['price']=$pro_price;$arr['pro_price']=$pro_price;
$arr['total']=$pro_price-$quan_price;
$arr['yun_id']=$l['yun_id'];
$arr['zt_addr']=$user['zt_addr'];
if($user['my_dft_yun']){//如果是用户上传运输方式
$arr['yun']=$user['my_dft_yun'];
}else{$arr['yun']='is_ps';}

$arr['c']='hd';
$arr['code']=md5(time());
$arr['hd_title']=$l['title'];
$arr['zk']=$l['zk'];

//用户的位置信息
if($user['latitude']){//用户上次的位置
$arr['latitude']=$user['latitude'];$arr['longitude']=$user['longitude'];
}else{//获取位置
$arr['latitude']=g('latitude');$arr['longitude']=g('longitude');//用户的
}
$arr['shop_latitude']=$shop['latitude'];$arr['shop_longitude']=$shop['longitude'];

$arr['shop_logo']=$shop['logo'];
if($arr['latitude'] and $arr['longitude'] and $shop['latitude']){
$arr['dis']=get_dis($arr['longitude'],$arr['latitude'],$shop['longitude'],$shop['latitude']);//获取位置信息
}
//邀请者 cps
$from_id=$user['cps_from_id'];
//按模式开启cps

//团长
if($l['cps']>0){
if($l['tz']==1){//仅团长

$sql='select * from '.$cfg['e'].'tz where shop_id="'.$shop['id'].'" and user_id="'.$from_id.'" and state=1';
$tz=db1($sql);
if($tz){//存在团长
$arr['from_id']=$from_id;
$arr['cps_bl']=number_format($l['cps']/100,2,'.','');;
}

}else{//所以人
$arr['from_id']=$from_id;
$arr['cps_bl']=number_format($l['cps']/100,2,'.','');
}
}


$id=insert($cfg['e'].'order',$arr);
//保存到商品数据库
foreach($cart as $v){
$v['order_id']=$id;
$v['hd_id']=$l['id'];
insert($cfg['e'].'order_pro',$v);
}
$err['id']=$id;
if($err_msg['is_empty'] or $err_msg['is_not_enough']){$err['err_msg']='抱歉，有部分商品缺货没有添加进入购物车，如有问题，请重新下单。';}
echo json_encode($err);
break;


case 'get_edit':
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
$err['set']=$set;
$shop=get_shop();
$err['shop']=$shop;

$err['bg']=$set['app_bg'];//背景图


if($id){//存在默认
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'"  and shop_id="'.$shop['id'].'" ';
$l=db1($sql);
if(!$l){err('没有找到活动，或者您没有编辑的权限，如有问题，请联系系统管理员。');}

}else{//不存在 读取最新没有发布的活动
$sql='select * from '.$cfg['e'].'hd where  shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'" ';
$sql.=' and state=0  order by id desc limit 1';
$l=db1($sql);
if($l){//如果存在活动

}else{
$id=new_hd();
$arr['id']=$id;
$l=$arr;
}
}

if($l){//处理数据
//存在未发布的
if($l['title']){$l['title']=base64($l['title']);}
if($l['info']){$l['info']=base64($l['info']);}
if($l['html']){$l['html']=unserialize(base64($l['html']));}

if($l['fen_data']){$l['fen_data']=unserialize(base64($l['fen_data']));}
if($l['nuo_data']){$l['nuo_data']=base64($l['nuo_data']);}

if($l['tag']){$l['tag']=unserialize(out($l['tag']));}else{$l['tag']=array();}

if($l['img']){
$img=explode('||',$l['img']);
foreach($img as $v){
if($v){
$im=get_img($v,'_240');
$imgx[]=$v;
$imgp[]=$im;

}
}
$l['img']=$imgx;
$l['imgs']=$imgp;
}




if($l['start']){
$start=[date('Y',$l['start']),date('m',$l['start']),date('d',$l['start']),date('H',$l['start']),date('i',$l['start'])];
$l['start']=$start;
}
if($l['end']){
$end=[date('Y',$l['end']),date('m',$l['end']),date('d',$l['end']),date('H',$l['end']),date('i',$l['end'])];
$l['end']=$end;
}


//处理商品信息
if($l['pro']){
$pro=unserialize(out($l['pro']));//活动商品

$l['pro']=$pro;
$err['pro']=get_pros($pro,100,0);
}

}

//对时间，图片等进行设置
$tag=$set['tag'];
if(!$tag){$tag=['今日必团','新品上市','今日秒杀','顺丰包邮','粉丝福利','每日特价'];}
foreach($tag as $v){
$err['info']['tag'][$v]=$v;
}



$show_name[1]=array('title'=>'头像和微信昵称','img'=>'logo','name'=>'我的昵称');
$show_name[2]=array('title'=>'隐藏头像和匿名','img'=>'shop_logo','name'=>'我***');
$show_name[3]=array('title'=>'只显示头像','img'=>'logo','name'=>'');
$show_name[4]=array('title'=>'只显示微信昵称','img'=>'','name'=>'我的昵称');
$show_name[5]=array('title'=>'只显示匿名','img'=>'','name'=>'我***');
$err['show_name']=$show_name;

$err['list']=$l;
$err['quan']=get_hd_quan($l['id'],1);//优惠券
//秒杀商品
if($l['miao_pro']){
$err['miao']=get_miao_pro($l['miao_pro']);
}

if($l['mz_pro']){
$err['mz']=get_mz_pro($l['mz_pro']);
}
echo json_encode($err);
break;



case 'hd_save'://活动保存
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

if(!$id){err('请指定货代');}
$data=p('data');if(!$data){err('没有保存的数据');}
$arr=json_decode($data,1);if(!$arr){err('没有找到保存的数据');}

$sql='select * from '.$cfg['e'].'hd where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
//如果是雇员，只能编辑自己的活动
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
//echo $sql;
$l=db1($sql);if(!$l){err('没有找到活动，或者您没有编辑的权限');}

if($arr['fen_data']){$arr['fen_data']=base64_encode(serialize($arr['fen_data']));}


if($arr['title'] ){$arr['title']=str_replace('
','',$arr['title']);//去掉换行符
$arr['temp']=$arr['title'];
$arr['title']=base64_encode($arr['title']);//编码
}


//开始和结束时间的计算
if($arr['start']){
$start=get_time($arr['start'][0].'-'.$arr['start'][1].'-'.$arr['start'][2].' '.$arr['start'][3].':'.$arr['start'][4].':0');
$arr['start']=$start;
}

if($arr['end']){
$end=get_time($arr['end'][0].'-'.$arr['end'][1].'-'.$arr['end'][2].' '.$arr['end'][3].':'.$arr['end'][4].':0');
$arr['end']=$end;
}

if($arr['start'] and $arr['end']){//都有输入时间
if($arr['start']>=$arr['end']){err('抱歉，开始时间不能大于结束时间');}

}else{
if($arr['end']){//如果只有结束时间
$l['end']=$end;
if(!$l['start']){$l['start']=time();$arr['start']=time();}

if($l['start']>0){
if($arr['end']<=$l['start']){err('抱歉，开始时间不能大于结束时间');}
}
}


if($arr['start']){
if(!$l['end']){$l['end']=$arr['start']+3600*24*7;$arr['end']=$l['end'];}
if($l['end']>0){
if($arr['start']>=$l['end']){err('抱歉，开始时间不能大于结束时间');}
}
}

}


//数据

if($arr['info']){$arr['info']=base64_encode($arr['info']);}
if($arr['html']){$arr['html']=base64_encode(serialize($arr['html']));}
if($arr['tag']){$arr['tag']=serialize($arr['tag']);}
if($arr['pro']){$arr['pro']=serialize($arr['pro']);}
//if($arr['hb_total']==''){$arr['hb_total']=0;}
//if($arr['hb_price']==''){$arr['hb_price']=0;}
//if($arr['hb_num']==''){$arr['hb_num']=0;}
if($l['state']>0){//如果有红包 那么必须关闭才能修改
if($arr['hb_total'] or $arr['hb_price'] or $arr['hb_num']){
err('抱歉，请停止活动后才能修改红包设置');
}
}

//折扣
if($arr['zk']){
if(!is_numeric($arr['zk'])){$arr['zk']=100;}else{
if($arr['zk']<20){$arr['zk']=20;}//最多折扣设置，最好在set中进行设置
}
}else{$arr['zk']=100;}


//商品数据
if($arr['pro_data']){//更换商品信息
foreach($arr['pro_data'] as $v){
if(is_numeric($v['id']) and $v['id']>0){

$sql='select * from '.$cfg['e'].'pro where id="'.a($v['id']).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
//if(!$is_adm){$sql.=' and shop_id="'.a($emp['list']['shop_id']).'" and user_id="'.$user['id'].'" ';}

$pro=db1($sql);
if($pro){
unset($v['id']);$pro_save=1;


if($v['old']){
if(!is_numeric($v['old'])){$v['old']=0;}    
}

if($v['price']){
if(!is_numeric($v['price'])){$v['price']=$pro['price'];}
}



//if($v['price']=='' or !is_numeric($v['price'])){$v['price']=$pro['price'];}

if($v['price']>0 and $v['old']>0){
if($v['price']>=$v['old']){
$err['err_msg']=base64($pro['title']).' 的划线价低于商品零售价';$pro_save=0;
}
}

if($v['total']=='' or !is_numeric($v['total'])){unset($v['total']);}

if($pro_save==1){
update($cfg['e'].'pro',$v,' where id="'.$pro['id'].'"');
}

}else{
$err['err_msg']='抱歉，有部分商品您没有权限进行修改，如果需要，可以自己创建商品。';
}

}}


unset($arr['pro_data']);
}



if($arr){update($cfg['e'].'hd',$arr,' where id="'.a($l['id']).'" ');}
echo json_encode($err);
break;








case 'pro_qucik_add'://快速添加商品
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('抱歉，您没有指定活动');}

$sql='select * from '.$cfg['e'].'hd where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}


$l=db1($sql);if(!$l){err('没有找到活动，或者您没有编辑的权限');}

if($l['pro']){
$pro=unserialize(out($l['pro']));
}else{$pro=array();}
$img=g('img');if(!$img){err('请指定图片');}
$imgs=explode('||',$img);
$arr['user_id']=$user['id'];

$arr['state']=1;
$arr['shop_id']=$shop['id'];
$arr['temp']='新建商品';
$arr['title']=base64_encode($arr['temp']);
$arr['date']=time();
$arr['total']=-1;
$arr['is_hide']=1;//隐藏
foreach($imgs as $v){
if($v){$arr['img']=$v;
$id=insert($cfg['e'].'pro',$arr);
$pro['item_'.$id]['id']=$id;
}
}

$arrs['pro']=serialize($pro);
update($cfg['e'].'hd',$arrs,' where id="'.$l['id'].'"');
sql_error();err('ok');
break;


case 'get_hd_pro'://活动商品列表
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

if(!$id){err('请指定活动');}

$sql='select * from '.$cfg['e'].'hd where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
//if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}


$l=db1($sql);if(!$l){err('没有找到活动，或者您没有编辑的权限');}
if($l['pro']){
$pro=unserialize(out($l['pro']));

}else{err('ok');}

$err['pro']=get_pros($pro,100,0);
echo json_encode($err);
break;

case 'hd_pro_del'://删除商品
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请选择活动');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'hd where id=:id and shop_id="'.$shop['id'].'" ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动，或者您没有编辑的权限');}
$pro_id=g('pro_id');
if(!$pro_id){err('没有指定删除的商品');}
if(!$l['pro']){err('ok');}
$pro=unserialize(out($l['pro']));
unset($pro['item_'.$pro_id]);
if(!$pro){$arr['pro']='';}else{$arr['pro']=serialize($pro);}
update($cfg['e'].'hd',$arr,' where id="'.$l['id'].'"');
$err['pro']=get_pros($pro,100,0);
echo json_encode($err);
break;

case 'hd_pro_edit'://编辑商品
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请选择活动');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'hd where id=:id and shop_id="'.$shop['id'].'" ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('ok');}
$data=json_decode($data,1);if(!$data or !is_array($data)){err('ok');}
if($l['pro']){$pro=unserialize(out($l['pro']));}else{unset($pro);}
foreach($data as $k=>$v){
$pro[$k]=$v;
}
$arr['pro']=serialize($pro);
update($cfg['e'].'hd',$arr,' where id="'.$l['id'].'"');
$err['pro']=get_pros($pro,100,0);
echo json_encode($err);
break;



case 'hd_quan_edit'://编辑优惠券信息
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定活动');}


$data=p('data');if(!$data){err('请输入优惠券信息');}
$data=json_decode($data,1);if(!$data or !is_array($data)){err('优惠券信息有误');}

$sql='select * from '.$cfg['e'].'hd where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}

$l=db1($sql);if(!$l){err('没有找到活动，或者您没有编辑的权限');}

if(!$data['price'] or !is_numeric($data['price'])){err('请输入优惠券金额');}
if(!$data['man'] or !is_numeric($data['man'])){err('请输入购物满金额');}
if(!$data['num'] or !is_numeric($data['num'] )){$data['num']=-1;}
$arr['price']=$data['price'];$arr['num']=$data['num'];$arr['man']=$data['man'];
//print_r($arr);exit;
if($arr['price']>=$arr['man']){err('满金额不能小于券金额');}
if($data['id']){//更新
if(!is_numeric($data['id'])){err('参数错误');}
$sql='select * from '.$cfg['e'].'quan where id="'.a($data['id']).'" and shop_id="'.$shop['id'].'" and hd_id="'.$l['id'].'"';
$quan=db1($sql);if(!$quan){err('没有找到优惠券信息，或您没有编辑的权限');}
//print_r($arr);
update($cfg['e'].'quan',$arr,' where id="'.$quan['id'].'"');
}else{//创建
$arr['shop_id']=$shop['id'];
$arr['hd_id']=$l['id'];
$arr['state']=1;
$arr['date']=time();
$arr['c']='满减券';
$id=insert($cfg['e'].'quan',$arr);
}

$err['quan']=get_hd_quan($l['id'],1);
if($err['quan']){$arrs['quan_num']=count($err['quan']);}else{$arrs['quan_num']=0;}
update($cfg['e'].'hd',$arrs,' where id="'.$l['id'].'"');
echo json_encode($err);
break;

case 'quan_del'://删除优惠券
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定需要删除的优惠券');}
$hd_id=g('hd_id');if(!$hd_id or !is_numeric($hd_id)){err('请指定活动');}

$sql='select * from '.$cfg['e'].'quan where id="'.a($id).'" and shop_id="'.$shop['id'].'" and hd_id="'.a($hd_id).'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'"';}
$l=db1($sql);if(!$l){err('没有找到优惠券，或者您没有编辑的权限');}
$sql='delete from '.$cfg['e'].'quan where id="'.$l['id'].'"';
sql_query($sql);
$err['quan']=get_hd_quan($hd_id,1);//活动优惠券数量
if($err['quan']){$arrs['quan_num']=count($err['quan']);}else{$arrs['quan_num']=0;}
update($cfg['e'].'hd',$arrs,' where id="'.$hd_id.'"');
err('ok');
break;

case 'hd_start'://开启活动
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$id=g('id');if(!$id){err('请指定活动');}

$sql='select * from '.$cfg['e'].'hd where id=:id and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
//set_share_img($l);//设置分享图
//exit;
if($l['state']==1){err('ok');}

//对活动信息的一些查找
if(!$l['title']){err('您没有编辑活动的标题，请输入一个优质的标题后继续');}
if(!$l['pro']){err('抱歉，您的活动中还没有设置商品');}
if($l['end']<time()){err('抱歉，您的活动已经结束了，请修改结束时候后继续');}
if(!$l['yun_id']){err('请选择一个合适的运费模板');}
if(!$l['img']){err('请至少选择一张主图图片。');}

$pro=unserialize(out($l['pro']));//活动商品
$pros=get_pros($pro,$l['zk']);
//对商品进行分析
$min=0;$max=0;
foreach($pros as $v){
if(!$v['title']){err('有部分商品名称为空');}
//if(!$v['price']){err('抱歉，您的部分商品价格为0');}
if($v['total']==0){$err['err_msg']='抱歉，您的部分商品库存数不足';}
if(!$min){$min=$v['price'];}
if($v['price']<$min){$min=$v['price'];}
if($v['price']>$max){$max=$v['price'];}
}
$arr['min']=$min;
if($l['zk']<100){$min=number_format($min*$l['zk']/100,2,'.','');$max=number_format($max*$l['zk']/100,2,'.','');}
$arr['max']=$max;
//秒杀
if($l['miao_price'] and $l['miao_pro']){
if($l['miao_price']<$min){$min=$l['miao_price'];}

}



if($shop){
$arr['shop_name']=base64_encode($shop['shop_name']);
$arr['shop_addr']=$shop['addr'];
$arr['logo']=$shop['logo'];
if($shop['latitude']){
$arr['latitude']=$shop['latitude'];
$arr['longitude']=$shop['longitude'];
}
}

//如果有微信红包
if($l['hb_price'] and $l['hb_num'] and $l['hb_total'] ){
$hb_num=$l['hb_total']-$l['hb_is_get'];
$hb_price=number_format($l['hb_price']*$hb_num,2,'.','');
//冻结资金
if($shop['balance']<$hb_price){err('抱歉，您的现金红包所需冻结资金不够，请充值或调整红包金额数量');}
//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['hd_id']=$l['id'];
$zd['nickname']=$user['nickname'];
$zd['logo']=$user['logo'];
//管理员账单
$zd['user_id']=$user['id'];
$zd['title']='现金红包冻结资金';
$zd['c']='资金冻结';
$zd['money']=-$hb_price;//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);


$sql='update '.$cfg['e'].'shop set freeze=freeze+'.$hb_price.' where id="'.$l['shop_id'].'"';
sql_query($sql);

}



$arr['state']=1;$arr['date']=time();
update($cfg['e'].'hd',$arr,'  where id="'.$l['id'].'"');

//增加1
$sql='update '.$cfg['e'].'shop set hd_num=hd_num+1 where id="'.$l['shop_id'].'"';
sql_query($sql);
$shop_arr['hd_bg']=$l['bg'];
$shop_arr['yun_id']=$l['yun_id'];
$shop_arr['yun_title']=$l['yun_title'];
$shop_arr['cps_bl']=$l['cps_bl'];
update($cfg['e'].'shop',$shop_arr,' where id="'.$l['shop_id'].'"');

if(!$err['err_msg']){
$err['err_msg']='您的活动已经成功开启，您可以转发活动到朋友圈或者公众号。';}

//创建活动消息模板
//get_activity_id($l);

set_share_img($l);//设置分享图
pm_add(1,$shop,'活动上线');

echo json_encode($err);
break;

case 'hd_stop'://停用活动
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定活动');}
$shop=get_shop();

$sql='select * from '.$cfg['e'].'hd where id=:id and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
if($l['state']==0){err('ok');}


//如果有微信红包 资金解冻
if($l['hb_price'] and $l['hb_num'] and $l['hb_total']){
$hb_num=$l['hb_total']-$l['hb_is_get'];
$hb_price=number_format($l['hb_price']*$hb_num,2,'.','');
if($hb_price>0){
//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['hd_id']=$l['id'];
$zd['nickname']=$user['nickname'];
$zd['logo']=$user['logo'];
//管理员账单
$zd['user_id']=$user['id'];
$zd['title']='现金红包冻结资金解冻';
$zd['c']='资金解冻';
$zd['money']=$hb_price;//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);

$sql='update '.$cfg['e'].'shop set freeze=freeze-'.$hb_price.' where id="'.$l['shop_id'].'"';
sql_query($sql);
}
}


$arr['state']=0;
update($cfg['e'].'hd',$arr,'  where id="'.$l['id'].'"');
//建1
$sql='update '.$cfg['e'].'shop set hd_num=hd_num-1 where id="'.$l['shop_id'].'"';
sql_query($sql);
pm_add(-1,$shop,'活动下线');
err('ok');
break;

case 'hd_del'://删除活动
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定活动');}

$sql='select * from '.$cfg['e'].'hd where id=:id and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到活动信息，或者您没有编辑的权限');}
if($l['state']!=0){err('请停用活动后继续');}
if($l['people_num']>0 or $l['order_num']>0){err('抱歉，您的活动存在订单，无法删除。');}
$sql='delete from '.$cfg['e'].'hd where id="'.$l['id'].'"';
sql_query($sql);
err('ok');
break;

case 'get_hd_quan'://获取活动优惠券
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('抱歉，请指定活动');}
$quan=get_hd_quan($id);
if(!$quan){err('ok');}
$my_quan=get_hd_my_quan($id);

if($my_quan){
foreach($my_quan as $v){
unset($quan['item_'.$v['quan_id']]);
}
}
if($quan){
$arr['user_id']=$user['id'];

foreach($quan as $v){
if($v['num']!=0){
$arr['shop_id']=$v['shop_id'];
$arr['hd_id']=$v['hd_id'];
$arr['price']=$v['price'];
$arr['man']=$v['man'];
$arr['c']=$v['c'];
$arr['state']=1;
$arr['date']=time();
$arr['end']=$v['end'];
$arr['quan_id']=$v['id'];
$arr['nickname']=$user['nickname'];
$arr['logo']=$v['logo'];
$arr['is_use']=0;
$arr['order_id']=0;

insert($cfg['e'].'quan_get',$arr);
$arrs['num']=$v['num']-1;
if($arrs['num']<=0){$arrs['num']=0;}
update($cfg['e'].'quan',$arrs,'where id="'.$v['id'].'"');
}
}
}
err('ok');
break;


case 'submit_msg'://提交留言
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定活动');}

$msgx=g('val');if(!$msgx){err('请输入您的留言内容');}
$arr['msg']=base64_encode($msgx);
$arr['user_id']=$user['id'];
$arr['date']=time();
$arr['hd_id']=$id;
$arr['logo']=$user['logo'];
$arr['nickname']=base64_encode($user['nickname']);

$sql='select * from '.$cfg['e'].'hd where  id="'.$id.'"';
$l=db1($sql);if(!$l){err('没有找到活动');}
if($l['state']==0){err('抱歉，该活动还没有上线，无法进行评论');}
$arr['shop_id']=$l['shop_id'];
$id=insert($cfg['e'].'msg',$arr);

$shop=get_hd_shop($l['shop_id']);
if($shop['wx_name']){
//发送消息给客服
//$d['jump_wxa']['path']='/pages/index/index';
//$d['jump_wxa']['query']='';
$d['path']='/pages/adm/msg';
$d['query']='hd_id='.$l['id'];

$re=get_url($d);
$re=json_decode($re,1);
$err['url']=$re['url_link'];
//新评价
$msg['touser']=$shop['wx_name'];
$msg['temp_id']='TLuzMVhrLMQiMhaxq6bvly0hUtFK_AeDWCyFHdnHxkI';
$msg['url']=$err['url'];
$msg_data['first']='您的活动有新的评价';
$msg_data['keyword1']['value']=$user['nickname'];
$msg_data['keyword2']['value']=$msgx;
$msg_data['keyword3']['value']=date('Y-m-d H:i:s',$order['time']);
$msg_data['remark']['value']='请点击进行评价确认和回复。';
$msg_data['remark']['color']='red';
$msg['data']=$msg_data;
wx_msg($msg);

}

echo json_encode($err);
break;

case 'get_more_msg'://获取更多活动信息
$id=g('id');if(!$id or !is_numeric($id)){err('ok');}
$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$err['msg']=get_hd_msg($id);
echo json_encode($err);
break;


case 'creat_new_hd'://创建或者复制活动
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$id=g('id');$c=g('c');if($c!='copy'){$c='new';}
if($c=='copy' and !$id){err('请指定需要复制的活动');}

if($id and $c=='copy'){$sql='select * from '.$cfg['e'].'hd where shop_id="'.$shop['id'].'" and id=:id';
$p['id']=$id;
$l=db1($sql,$p);
if(!$l){err('没有找到活动');}
unset($l['user_id'],$l['id']);
$l['start']=time()+1800;
$l['end']=time()+3600*24*3;
$l['user_id']=$user['id'];
$l['logo']=$shop['logo'];
$l['state']=0;
$l['order_num']=0;
$l['people_num']=0;
$l['view']=0;
$l['activity_id']='';
$id=insert($cfg['e'].'hd',$l);

}else{
$id=new_hd();
}
$err['id']=$id;
echo  json_encode($err);
break;




case 'get_cart'://获取订单数据
$user=is_login();
if(!$user){err('no_login');}
$err['set']=$set;
$err['user']=$user;
$err['can_pay']=1;
$id=g('id');if(!$id){err('抱歉，请指定订单');}
//$app=get_app();
//$err['app']=$app;

$sql='select * from '.$cfg['e'].'order where  id="'.a($id).'" and user_id="'.$user['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到订单');}
//if($l['user_id']!=$user['id']){err('抱歉，您没有查看订单的权限');}
if($l['msg']){$l['msg']=base64($l['msg']);}
$shop=get_hd_shop($l['shop_id']);

if($l['pro']){//位置
$l['region']=[$l['pro'],$l['city'],$l['dist']];
}else{
$l['region']=[$shop['pro'],$shop['city'],$shop['dist']];
}

if($l['state']>0){$err['err_msg']='您的订单已经支付完成，如有问题，请联系我们。';$err['can_pay']=0;}else{
if($l['pay_end']<time() and $l['pay_end']>0){$err['err_msg']='抱歉，您的订单已经超时，请重新下单。';$err['can_pay']=0;}
}

//活动
if($l['hd_id']){
$sql='select * from '.$cfg['e'].'hd where id="'.$l['hd_id'].'" and shop_id="'.$l['shop_id'].'" ';
$hd=db1($sql);
if(!$hd){err('抱歉，没有找到相应的活动,可能该活动已经停止。');}
if($hd['state']!=1){$err['err_msg']='抱歉，该活动还没有开始，您无法进行支付。';$err['can_pay']=0;}
if($hd['end']<time()){$err['err_msg']='抱歉，活动已经结束。';$err['can_pay']=0;}
if($hd['start']>time()){$err['err_msg']='抱歉，该活动还没有开始。';$err['can_pay']=0;}

if($hd['fen_data']){$hd['fen_data']=unserialize(base64($hd['fen_data']));}

$err['hd']=$hd;
}

//商品
$sql='select * from '.$cfg['e'].'order_pro where order_id="'.$l['id'].'" order by id asc';
$pros=db($sql);
if($pros){
foreach($pros as $k=>$v){
$v['title']=base64($v['title']);
if($v['c']=='赠品'){$mz=$v;}
if($v['c']=='秒杀'){$miao=$v;}
if($v['c']=='pro'){$pro[]=$v;}
}
}


$err['miao']=$miao;
$err['pro']=$pro;
$err['mz']=$mz;




//获取订单运费
if(!$l['yun_id']){
if($shop['yun_id']){
$sql='select * from '.$cfg['e'].'yun where id="'.$shop['yun_id'].'" and shop_id="'.$shop['id'].'"';
}else{
$sql='select * from '.$cfg['e'].'yun where shop_id="'.$shop['id'].'" order by is_dft desc,id asc limit 1';
}
}else{//存在运费模板
$sql='select * from '.$cfg['e'].'yun where id="'.$l['yun_id'].'" and shop_id="'.$shop['id'].'" ';
}
$yun=db1($sql);
if(!$yun){err('抱歉，没有找到合适的运费模式，请联系管理员处理。');}

if(!$yun[$l['yun']]){//不存在的运输条件，则选择其他
if($yun['is_ps']){$cart_arr['yun']='is_ps';}else{
if($yun['is_zt']){$cart_arr['yun']='is_zt';}else{
$cart_arr['yun']='is_kd';
}}
update($cfg['e'].'order',$cart_arr,' where id="'.$l['id'].'"');
$l['yun']=$cart_arr['yun'];
}


$yun_info=get_yun($yun,$l);//获取运费信息

//print_r($yun_info);
//err($yun_info['err']);
if($yun_info['err']=='ok'){
$err['zt_addr']=$yun_info['ztd'];//自提点
$l['dis']=$yun_info['dis'];

if($yun_info['zt_addr']>0){
$l['zt_addr']=$yun_info['zt_addr'];//自提点
if($yun_info['zt']){
$shop['shop_name']=$yun_info['zt']['title'];
$shop['phone']=$yun_info['zt']['phone'];
$shop['pro']=$yun_info['zt']['pro'];$shop['city']=$yun_info['zt']['city'];$shop['dist']=$yun_info['zt']['dist'];
$shop['addr']=$yun_info['zt']['addr'];
$shop['latitude']=$yun_info['zt']['latitude'];$shop['longitude']=$yun_info['zt']['longitude'];
}

}
    
$arrs['yun_id']=$yun_info['yun_id'];
$arrs['yun_price']=$yun_info['yun_price'];
//$arrs['dis']=$yun_info['dis'];
//err($arrs['dis']);
if($yun_info['latitude']){$arrs['latitude']=$yun_info['latitude'];}//如果有经纬度，则保持经纬度
if($yun_info['longitude']){$arrs['longitude']=$yun_info['longitude'];}


//err($arrs['yun_price']);
if($arrs['yun_id']==$l['yun_id'] and $arrs['yun_price']==$l['yun_price']){//不需要更新
}else{$need_update=1;
$l['yun_id']=$arrs['yun_id'];$l['yun_price']=$arrs['yun_price'];
}

}

if($yun_info['err_msg']){
$err['err_msg']=$yun_info['err_msg'];
$err['can_pay']=0;
}

if(!$yun['info']){
$yun['info']=get_yun_info($yun);
}

$err['yun']=$yun;
//距离
if($l['dis']>0){
if($l['dis']>1000){
$l['dis']=number_format($l['dis']/1000,2,'.','').'千米';
}else{
$l['dis']=$l['dis'].'千米';
}
}else{$l['dis']='';}

//优惠券
$shop_id=$shop['id'];
$quan=get_hd_my_quan($hd['id']);

if($quan){//unset($arrs);
$hav_quan=0;
foreach($quan as $k=>$v){
if($v['state']!=1){unset($quan[$k]);}else{
if($v['id']==$l['quan_id']){$hav_quan++;}
}
}

if(!$hav_quan){//没有找到优惠券
$arrs['quan_id']=0;$arrs['quan_price']=0;$arrs['quan_info']='没有可用优惠券';$need_update=1;
if($quan){
foreach($quan as $v){
if($l['price']>=$v['man']){
$arrs['quan_id']=$v['id'];$arrs['quan_price']=$v['price'];$arrs['quan_info']='满'.$v['man'].'减'.$v['price'].'元';
$l['quan_id']=$v['id'];$l['quan_price']=$v['price'];$l['quan_info']=$arrs['quan_info'];
}
}}
}
$err['quan']=$quan;
}else{
$arrs['quan_id']=0;$arrs['quan_info']='';$arrs['quan_price']=0;$need_update=1;//没有优惠券
$l['quan_id']=0;$l['quan_price']='0.00';$l['quan_info']='没有可用优惠券';
}
if(!$l['quan_id']){$l['quan_info']='没有可用优惠券';}

if($arrs and $need_update){
$arrs['total']=$l['price']+$l['yun_price']-$l['quan_price'];
$l['total']=number_format($arrs['total'],2,'.','');
update($cfg['e'].'order',$arrs, 'where id="'.$l['id'].'"');
}

$err['shop']=$shop;
$err['list']=$l;//订单数据

//消息模板
$err['temp']=get_temp('订单提交');


echo json_encode($err);
break;



case 'set_quan'://设置优惠券
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');if(!$id or !is_numeric($id)){err('请指定订单');}
$quan_id=g('quan_id');if(!$quan_id or !is_numeric($quan_id)){err('请指定优惠券');}

$sql='select * from '.$cfg['e'].'order where id=:id and  user_id="'.$user['id'].'" ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到订单');}
if($l['state']>0){err('您的订单已经支付，无法设置优惠券');}
$sql='select * from '.$cfg['e'].'quan_get where id=:id and shop_id="'.$l['shop_id'].'" and user_id="'.$user['id'].'" and state=1 ';
$p['id']=$quan_id;
$quan=db1($sql,$p);
if(!$quan){err('抱歉，没有找到优惠券');}
$arr['quan_id']=$quan['id'];$arr['quan_price']=$quan['price'];$arr['quan_info']='满'.$quan['man'].'减'.$quan['price'].'元';
$arr['total']=$l['price']+$l['yun_price']-$arr['quan_price'];
update($cfg['e'].'order',$arr,'where id="'.$l['id'].'"');
err('ok');
break;

case 'order_msg_submit'://提交订单留言
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');if(!$id or !is_numeric($id)){err('请指定订单');}
if(!$user){$user=is_login();}
$sql='select * from '.$cfg['e'].'order where id=:id  and user_id="'.$user['id'].'" ';
$p['id']=$id;
$l=db1($sql,$p);if(!$l){err('没有找到订单');}
if($l['state']>1){err('您的订单已经配送，无法修改订单留言');}
$arr['msg']=g('val');if(!$arr['msg']){err('请输入留言内容');}
$arr['msg']=base64_encode($arr['msg']);
update($cfg['e'].'order',$arr,'where id="'.$l['id'].'"');
err('ok');
break;







case 'pay'://支付
$user=is_login(1);
if(!$user){err('no_login');}
$id=g('id');if(!$id){err('请指定需要支付的订单');}

$sql='select * from '.$cfg['e'].'order where id=:id and user_id="'.$user['id'].'" ';
$p['id']=$id;
$order=db1($sql,$p);if(!$order){err('抱歉，没有找到订单');}
if($order['state']>0){err('抱歉，该订单已经支付');}
if($order['pay_end']<time() and $order['pay_end']>0){err('抱歉，该订单支持超时，请重新下单即可。');}
$shop=get_hd_shop($order['shop_id']);

if(!$shop){err('没有找到门店');}
if($shop['state']==99){err('抱歉，您的门店状态异常，请联系管理员进行处理');}
if($shop['state']!=1){err('您的门店还没有上线，请上线后重试');}

if($order['hd_id']){
$sql='select * from '.$cfg['e'].'hd where id="'.$order['hd_id'].'"  ';
$hd=db1($sql);
if(!$hd){err('抱歉，没有找到对应的活动。');}
if($hd['state']!=1){err('抱歉，该活动已经停止或者还没有启动。');}
if($hd['start']>time()){err('抱歉，该活动还没有开始，支付失败。');}
if($hd['end']<time()){err('抱歉，本活动已经结束，下次早点哦。');}
if(!$hd['pro']){err('该活动商品数据错误，请联系我们');}
$hd['title']=base64($hd['title']);
$hd['pro']=unserialize(out($hd['pro']));//活动商品

$hd_pros=get_pros($hd['pro'],$hd['zk']);//活动商品

if(!$hd_pros){err('没有找到活动商品');}
}

//对订单数据进行核实
if($order['quan_id']){
$sql='select * from '.$cfg['e'].'quan_get where id="'.$order['quan_id'].'" and user_id="'.$user['id'].'" and state=1';
$quan=db1($sql);
if(!$quan){err('抱歉，没有找到优惠券，或者您的优惠券已经使用，请重新下单或者更换优惠券后重试');}
}
if(!$order['name']){err('抱歉，联系人不能为空');}
if(!$order['phone']){err('为了更好的服务您，请输入您的联系电话');}

if($order['yun']=='is_ps' or $order['yun']=='is_kd'){
if(!$order['pro']){err('请选择您的地区');}
if(!$order['addr']){err('请输入您的配送地址，您也可以点击导航图标，选择您的地址');}
}


//获取amd信息


if(!$set['mch_id'] or !$set['pay_key']){err('没有设置有效的商户号');}

//查看商品库存和缓存
if($redis and $hd){
$hd_cache=$redis->get('hdcache_'.$hd['id']);
if($hd_cache){$hd_cache=unserialize(out($hd_cache));}
if(!$hd_cache or !is_array($hd_cache)){$hd_cache=[];}
//print_r($hd_cache);
}

$pay_timeout=65;

$sql='select * from '.$cfg['e'].'order_pro where  order_id="'.$order['id'].'" order by id asc';
$order_pros=db($sql);
if($order_pros){
foreach($order_pros as $v){
$v['title']=base64($v['title']);
switch($v['c']){
case '秒杀':
$pro=get_pro_item($v['pro_id']);
if(!$pro){err($v['title'].' 没有找到秒杀商品');}
$cache_key='miao_'.$pro['id'];
if($hd){
$max=$hd['miao_total'];$is_have=0;
if($max>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $m=>$n){
if($n['date']<time()){unset($hd_cache[$cache_key][$m]);}else{
$num+=$n['num'];
}
}

$max=$max-$num;
if(!$max or $max<0){$max=0;}
if($max==0){err('抱歉，秒杀商品已经抢光了。');}
//缓存
}
$hd_cache[$cache_key][$user['id'].'_'.time()]=array('id'=>$order['id'],'user_id'=>$user['id'],'num'=>1,'date'=>time()+$pay_timeout);//60秒后超时

}
}

break;//秒杀
    
case 'pro':
//查找商品
if($hd){
$pro=$hd_pros['item_'.$v['pro_id']];
if(!$pro){err($v['title'].' 没有找到商品信息');}
}else{//不是活动下单
$pro=get_pro_item($v['pro_id']);
if(!$pro){err($v['title'].' 没有找到商品信息');}
$pros['item_'.$pro['id']]=$pro;
}

if($pro['total']==0){err('抱歉，'.$v['title'].'已经售罄，请重新下单。');}//商品已经售罄，抛出错误
if($pro['data']){//存在参数 对里面的数据数量进行分析
foreach($pro['data'] as $j=>$t){//一级分类
if($t['item']){
foreach($t['item'] as $m=>$n){
$max=$n['num'];$is_cache=0;$cache_key='pro_'.$pro['id'].'_'.$j.'_'.$m;
if($v['cache_key']==$cache_key){
if($max==0){err('抱歉，'.$v['title'].' '.$j.' '.$n['title'].' 商品已经售罄，请重新下单后继续');}

if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $x=>$z){//echo ($z['date']-time()).' <br>';
if($z['date']<time()){unset($hd_cache[$cache_key][$x]);}else{//超时
//print_r($z);
$is_cache+=$z['num'];
}

}
if($hd){}else{$max=$max-$is_cache;}
if($max<=0){err('抱歉，'.$v['title'].' '.$j.' '.$n['title'].' 商品已经抢光了，请稍后下单重试');}

}
//缓存

$hd_cache[$cache_key][$user['id'].'_'.time()]=array('id'=>$order['id'],'user_id'=>$user['id'],'num'=>$v['num'],'date'=>time()+$pay_timeout);//60秒后超时
}

}//内部item

}else{//不存在子分类
$max=$t['num'];$is_cache=0;$cache_key='pro_'.$pro['id'].'_'.$j;
if($v['cache_key']==$cache_key){
if($max==0 and $cache_key=$v['cache_key']){err('抱歉，'.$v['title'].' '.$j.' 商品已经售罄，请重新下单后继续');}
if($max>0){//存在数量

if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $x=>$z){
if($z['date']<time()){unset($hd_cache[$cache_key][$x]);}else{//超时
$is_cache+=$z['num'];//echo $is_cache;
}

}
if($hd){}else{$max=$max-$is_cache;}
if($max<=0 and $cache_key=$v['cache_key']){err('抱歉，'.$v['title'].' '.$j.' 商品已经抢光了，请稍后下单重试');}

}

//缓存

$hd_cache[$cache_key][$user['id'].'_'.time()]=array('id'=>$order['id'],'user_id'=>$user['id'],'num'=>$v['num'],'date'=>time()+$pay_timeout);//60秒后超时
}
}//max>0

}//else
}
}else{//不存在参数
$cache_key='pro_'.$pro['id'];
if($pro['total']>0){
$max=$pro['total'];$is_cache=0;
if($hd_cache[$cache_key]){//存在已经缓存数据
foreach($hd_cache[$cache_key] as $m=>$n){
if($n['date']>time()){
$is_cache+=$n['num'];
}else{//已经超时，进行删除
 unset($hd_cache['pro_'.$pro['id']][$m]);$cache_save=1;
}
}
//echo $max.' '.$is_cache;
if($hd){}else{$max=$max-$is_cache;}
if($max<=0){err($pro['title'].'已经抢光了');}
}//存在缓存
//对数据进行缓存

$hd_cache[$cache_key][$user['id'].'_'.time()]=array('id'=>$order['id'],'user_id'=>$user['id'],'num'=>$v['num'],'date'=>time()+$pay_timeout);//60秒后超时
//print_r($hd_cache);
}
}

break;

case '赠品'://满赠
$pro=get_pro_item($v['pro_id']);
if(!$pro){err($v['title'].' 没有找到赠品信息');}
$cache_key='mz_'.$pro['id'];
if($hd){
$max=$hd['mz_total'];$is_have=0;
if($max>0){
if($hd_cache[$cache_key]){
foreach($hd_cache[$cache_key] as $m=>$n){
if($n['date']<time()){unset($hd_cache[$cache_key][$m]);}else{
$num+=$n['num'];
}
}

$max=$max-$num;
if(!$max or $max<0){$max=0;}
if($max==0){err('抱歉，赠品已经全部赠送完成。');}
//缓存
}
$hd_cache[$cache_key][$user['id'].'_'.time()]=array('id'=>$order['id'],'user_id'=>$user['id'],'num'=>1,'date'=>time()+$pay_timeout);//60秒后超时

}
}

break;//秒杀
}
}
}

if($redis and $hd){
$time=$hd['end']-time();if($time<=0){$time=3600;}
r_set('hdcache_'.$hd['id'],$hd_cache,$time);
}


$total=number_format($order['total'],2,'.','')*100;

if($cfg['debug']==1){$total=rand(10,20);}//随机金额


$err['total']=$total;
//$total=1;
$pay_data['notify_url']=$cfg['notify_url'];
$arrs['out_trade_no']=$order['id'].'_'.time();
$d['time_expire']=time()+63;//1分钟后截止支付
if($set['profit_sharing']){$arrs['profit_sharing']=1;}//是否开启分账


update($cfg['e'].'order',$arrs,' where id="'.$order['id'].'"');
$pay_data['out_trade_no']=$arrs['out_trade_no'];
$pay_data['body']='接龙付款';
$re=pay($total,$pay_data);
$err['pay']=$re;
echo json_encode($err);
break;

case 'clean_order'://清除缓存
$user=is_login();if(!$user){err('ok');}
if(!$redis){err('ok');}
$shop=get_shop();
if(!$id){err('ok');}
$sql='select * from '.$cfg['e'].'order where id="'.a($id).'" and user_id="'.$user['id'].'" ';
$l=db1($sql);if(!$l){err('ok');}

$hd_cache=$redis->get('hdcache_'.$l['hd_id']);if(!$hd_cache){err('ok');}
if($hd_cache){$hd_cache=unserialize(out($hd_cache));}
if(!$hd_cache or !is_array($hd_cache)){err('ok');}
foreach($hd_cache as $k=>$v){
if($v['date']<time() or $v['id']=$l['id']){unset($hd_cache[$k]);}
}
r_set('hdcache_'.$l['hd_id'],$hd_cache,3600*24*7);
err('ok');
break;


case 'get_order_list'://活动订单列表
$user=is_login();
if(!$user){err('no_login');}
//$shop=get_shop();
$err['set']=$set;
$state=g('state');
if(!is_numeric($state) or !$state){$state=0;}

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=10;$start=($page-1)*$page_num;

$sql='select * from '.$cfg['e'].'order where  user_id="'.$user['id'].'"  ';
if($state){
if($state==99){
$sql.=' and state>=80 ';
}else{
$sql.=' and state="'.$state.'" ';}
}else{
$sql.=' and state>0 ';
}

$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);

$latitude=g('latitude');
$longitude=g('longitude');
if($l){
foreach($l as $v){
$v['shop_name']=base64($v['shop_name']);
$v['date']=date('Y年m月d日 H:i:s',$v['date']);
if($v['pay_date']){$v['pay_date']=date('Y年m月d日 H:i:s',$v['pay_date']);}
if($v['finish_date']){$v['finish_date']=date('Y年m月d日 H:i:s',$v['finish_date']);}
if($v['tui_date']){$v['tui_date']=date('Y年m月d日 H:i:s',$v['tui_date']);}
$v['pay_price']=$v['pay_price']/100;
if($v['hd_title']){$v['hd_title']=base64($v['hd_title']);}
$v['pros']=get_order_pro($v['id']);
switch($v['yun']){
case 'is_zt':$v['how_yun']='门店自提';break;
case 'is_ps':$v['how_yun']='配送';break;
case 'is_kd':$v['how_yun']='快递';break;
}

switch($v['state']){
case 1:$v['state_text']='已付款';break;
case 2:$v['state_text']='已发货';break;
case 5:$v['state_text']='已配送';break;
case 10:$v['state_text']='已完成';break;
case 80:$v['state_text']='申请退款';break;
case 88:$v['state_text']='申请退款';break;
case 99:
if($v['out_refund_no']){$v['state_text']='已退款';}else{$v['state_text']='申请售后';}
break;
}
if($v['tui_pirce']>0){$v['state_text']='退款';$v['state']=99;}



if($latitude and $v['shop_latitude']){
$v['dis']=get_dis($longitude,$latitude,$v['shop_longitude'],$v['shop_latitude'],1);
if($v['dis']>1000){$v['dis']=number_format($v['dis']/1000,1,'.','').'km';}else{$v['dis']=$v['dis'].'米';}

}
if(!$v['dis']){$v['dis']='';}




$list['item_'.$v['id']]=$v;
}


$err['list']=$list;
}

echo json_encode($err);

break;


case 'get_adm_order'://获取订单
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$err['set']=$set;
$state=g('state');
if(!is_numeric($state) or !$state){$state=0;}
$hd_id=g('hd_id');if(!$hd_id or !is_numeric($hd_id)){$hd_id=0;}

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=10;$start=($page-1)*$page_num;

$sql='select * from '.$cfg['e'].'order where  shop_id="'.$shop['id'].'"  ';
if($hd_id>0){$sql.=' and hd_id="'.$hd_id.'" ';}
$key=g('key');
if($key){$sql.=' and (name like "%'.a($key).'%" or phone like "%'.a($key).'%") ';}
$yun=g('yun');
if($yun){$sql.=' and yun="'.$yun.'" ';}
switch($state){
default:$sql.=' and state>0 ';break;
case 1:$sql.=' and state=1 ';break;
case 5:$sql.=' and state=5 ';break;
case 10:$sql.=' and state=10 ';break;
case 80:$sql.=' and state in (80,88) ';break;
case 99:$sql.=' and state=99 ';break;
}

$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);

$latitude=g('latitude');
$longitude=g('longitude');
if($l){
foreach($l as $v){
$v['shop_name']=base64($v['shop_name']);
$v['nickname']=base64($v['nickname']);
$v['date']=date('Y年m月d日 H:i:s',$v['date']);
if($v['pay_date']){$v['pay_date']=date('n月j日 H:i',$v['pay_date']);}
if($v['finish_date']){$v['finish_date']=date('Y年m月d日 H:i:s',$v['finish_date']);}
if($v['tui_date']){$v['tui_date']=date('Y年m月d日 H:i:s',$v['tui_date']);}
$v['pay_price']=$v['pay_price']/100;
if($v['hd_title']){$v['hd_title']=base64($v['hd_title']);}
$v['pros']=get_order_pro($v['id']);
switch($v['yun']){
case 'is_zt':$v['how_yun']='客户自提';break;
case 'is_ps':$v['how_yun']='配送';break;
case 'is_kd':$v['how_yun']='快递';break;
}

switch($v['state']){
case 1:$v['state_text']='已付款';break;
case 2:$v['state_text']='已发货';break;
case 5:$v['state_text']='已配送';break;
case 10:$v['state_text']='已完成';break;
case 80:$v['state_text']='申请退款';break;
case 88:$v['state_text']='申请退款';break;
case 99:
if($v['out_refund_no']){$v['state_text']='已退款';}else{$v['state_text']='申请售后';}
break;
}
if($v['tui_pirce']>0){$v['state_text']='退款';$v['state']=99;}



if($v['latitude'] and $v['shop_latitude']){
$v['dis']=get_dis($v['longitude'],$v['latitude'],$v['shop_longitude'],$v['shop_latitude'],1);
if($v['dis']>1000){$v['dis']=number_format($v['dis']/1000,1,'.','').'km';}else{$v['dis']=$v['dis'].'米';}

}
if(!$v['dis']){$v['dis']='';}

$list['item_'.$v['id']]=$v;
}

$err['list']=$list;
}

echo json_encode($err);
break;

case 'adm_order_change'://更新订单数据
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and shop_id="'.$shop['id'].'" and state in (1,2,5,80,88)';
$l=db1($sql);
if(!$l){err('没有找到订单，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['send_data'];
foreach($c as $v){
$arr[$v]=$data[$v];
}
if(!$arr){err('数据错误');}
if($arr['send_data']){$arr['send_data']=serialize($arr['send_data']);}
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'get_qr_data':
$user=is_login();
if(!$user){$user=[];}
$page=g('page');if(!$page){
if($cfg['debug']){$page='pages/index/index';}else{$page='pages/index/detail';}
}
$server='https://'.$cfg['domain'].'/'.$cfg['path'].'/';if(!$id){$id='';}
$d=array('scene'=>'id='.$id.'&f='.$user['id'],'width'=>400,'is_hyaline'=>true,'page'=>$page,'check_path'=>false);
$qr=get_qr($d);
if(strlen($qr)>100){
$path='./cache/'; 
if(!file_exists($path)){mkdir($path);}
$path='./cache/qr/';
if(!file_exists($path)){mkdir($path);}
$file='hdx_'.$id.'_u_'.$user['id'].'.png';
if(!file_exists($path.$file)){
$qr_file=$path.$file;
$newFile = fopen($qr_file,"w"); //打开文件准备写入
fwrite($newFile,$qr); //写入二进制流到文件
fclose($newFile); //关闭文件
}
}else{err('二维码生成失败');}
$err['qr']=$server.'cache/qr/'.$file;
echo json_encode($err);
break;






case 'get_set':
$set=get_set();
$err['set']=$set;
echo json_encode($err);
break;


case 'wx_login'://微信用户登录
$code=g('code');
if(!$code){err('登录参数错误');}

$url='https://api.weixin.qq.com/sns/jscode2session?appid='.$cfg['wx_xcx_appid'].'&secret='.$cfg['wx_xcx_secret'].'&js_code='.$code.'&grant_type=authorization_code';
$body=curlPost($url,'','');

$x=json_decode($body,true);

if($x['openid']){
$arr['openid']=$x['openid'];//微信登录名
$arr['unionid']=$x['unionid'];
$arr['nickname']=g('nickName');
$arr['nickname']=base64_encode($arr['nickname']);//base64
$arr['logo']=g('logo');
$arr['last_login']=time();
$arr['session']=md5(time());
$arr['wx_nickName']=g('nickName');
$sql='select * from '.$cfg['e'].'users where openid="'.a($arr['openid']).'"   order by id desc';
$user=db1($sql);
if($user){

$arrs['last_login']=time();
$arrs['session']=md5(time());
$user['session']=$arrs['session'];
update($cfg['e'].'users',$arrs,'where id='.$user['id']);sql_error();
$user['err']='ok';
r_set($cfg['path'].'user_'.$user['id'],'',600);
echo json_encode($user);
exit;
}else{//不存在用户
$arr['date']=time();
$arr['phone']=g('phone');
$arr['from_id']=g('from_id');if(!$arr['from_id']){$arr['from_id']=0;}//是否被邀请
$i=insert($cfg['e'].'users',$arr);//创建新用户
$arr['id']=$i;
$arr['err']='ok';
//积分邀请
if($set['share_jifen']>0 and $arr['from_id']){
jifen($set['share_jifen'],$arr['from_id'],'邀新','用户id:'.$id);
}

if($id){//同城评级
$shop=get_hd_shop($id);
pm_add(2,$shop,'邀请新人');
//用户的位置进行更新
$arrs['pro']=$shop['pro'];
$arrs['city']=$shop['city'];
$arrs['dist']=$shop['dist'];
update($cfg['e'].'users',$arrs,' where id="'.$arr['id'].'"');

}


$user=$arr;
if($set['go_to_new']){$arr['is_new']=1;}//是否跳转到修改用户信息页面
echo json_encode($arr);
}

}else{//登录错误
err($x['errmsg']);
}

break;

case 'get_img'://获取图片
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$c=g('c');if(!$c){$c='pro';}
$sql='select * from '.$cfg['e'].'img where  shop_id="'.$shop['id'].'" ';
$page=g('page');
if(!$page){$page=1;}
$page_num=36;$start=($page-1)*$page_num;
if($c=='sys'){

}else{
$sql.=' and user_id="'.$user['id'].'" ';
if($c){$sql.=' and c=:c '; $p['c']=$c;}

if($page==1){
$sql1='select s from '.$cfg['e'].'img where shop_id="'.$shop['id'].'" and  user_id="'.$user['id'].'" group by s';
$err['s']=db($sql1);
}

}
$ss=g('s');
if($ss){$sql.=' and s=:s ';$p['s']=$ss;}

$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);
if($l){
foreach($l as $k=>$v){
if($v['url']){
$v['img']=$v['url'];
}else{
$v['img']=$v['id'].'|'.$v['ext'];}
$v['img_240']=get_img($v['img'],'_240');
$v['img_480']=get_img($v['img'],'_480');
$v['img_750']=get_img($v['img'],'_750');
$v['img_1080']=get_img($v['img'],'_1080');
$list['item_'.$v['id']]=$v;
}
$err['list']=$list;
}

echo json_encode($err);

break;

case 'set_img_s':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
if(!$data['s']){err('请输入图片分类');}
$img=p('img');if(!$img){err('请选择图片');}
$img=json_decode($img,1);if(!$img){err('请选择图片');}
$c=g('c');if($c=='sys'){err('抱歉，系统图片无法修改分类');}
foreach($img as $v){
$id.=$v['id'].',';
}
$arr['s']=$data['s'];
update($cfg['e'].'img',$arr,' where shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'" and c!="sys" and id in ('.$id.'0)');

$sql1='select s from '.$cfg['e'].'img where shop_id="'.$shop['id'].'" and  user_id="'.$user['id'].'" group by s';
$err['s']=db($sql1);
echo json_encode($err);
break;




case 'upload_file':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop(1);
$file=$_FILES['file'];
if(!$file){err('没有找到上传文件');}
$arr['user_id']=$user['id'];
$arr['date']=time();
$arr['state']=1;
$arr['name']=g('name');
$arr['shop_id']=$shop['id'];
$f=explode('.',$file['name']);
$arr['ext']=$f[1];
$arr['size']=g('size');if(!$arr['size']){$arr['size']=0;}
$arr['c']=g('c');if(!$arr['c']){$arr['c']='pro';}
$id=insert($cfg['e'].'img',$arr);
if($id){
//保存文件
$path='./image/';if(!file_exists($path)){mkdir($path,0700);}
$path='./image/'.ceil($id/1000).'/';
if(!file_exists($path)){mkdir($path,0700);}
move_uploaded_file($file['tmp_name'],$path.$id.'.'.$arr['ext']);
if($arr['c']=='logo'){
//如果是logo 
logo_img($path.$id.'.'.$arr['ext']);
}
resize_img($path,$id.'.'.$arr['ext'],$id,$arr['ext'],array(240,480,750,1080));

}
$ids=g('id');
$img=$id.'|'.$arr['ext'];
$img_480=get_img($img,'_480');
$img_240=get_img($img,'_240');
$img_750=get_img($img,'_750');
echo 'ok'.'||'.$ids.'||'.$img.'||'.$img_240.'||'.$img_480.'||'.$img_750.'||'.$id;
break;


case 'img_del'://删除图片
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');
if(!$id){err('请指定需要删除的图片');}
$sql='select * from '.$cfg['e'].'img where id in ('.$id.'0) and user_id="'.$user['id'].'" ';
$l=db($sql);

if(!$l){err('没有找到图片');}

foreach($l as $v){
$path='./image/'.ceil($v['id']/1000).'/';
//echo $path.$v['id'].'.'.$v['ext'];
@unlink($path.$v['id'].'.'.$v['ext']);
@unlink($path.$v['id'].'_240.'.$v['ext']);
@unlink($path.$v['id'].'_480.'.$v['ext']);
@unlink($path.$v['id'].'_750.'.$v['ext']);
@unlink($path.$v['id'].'_1080.'.$v['ext']);
$sql='delete from '.$cfg['e'].'img where id="'.$v['id'].'"';
sql_query($sql);
}
$err['list']=$l;
echo json_encode($err);

break;


case 'get_pro_list'://获取商品数据
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$err['shop']=$shop;
$err['set']=$set;
$sql='select id,img,title,price,old,total,c,s,daiwei,state,info,is_new,is_hot from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" ';
$c=g('c');
if($c){
if($c=='no_val'){
$sql.=' and c is Null ';
}else{

$sql.=' and c=:c';$p['c']=$c;}}
$key=g('key');
if($key){$sql.=' and temp like "%'.a($key).'%"';}
$page=g('page');
if(!$page){$page=1;}$page_num=20;
$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);
if($l){
foreach($l as $v){
if($v['img']){
$v['im']=$v['img'];
$v['img']=get_img($v['img'],'_240');}
$v['title']=base64($v['title']);
$v['info']=base64($v['info']);

$list['item_'.$v['id']]=$v;
}

$err['list']=$list;
}

$sql='select c from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" group by c order by id desc';
$cc=db($sql);
if($cc){
foreach($cc as $v){
if($v['c']){
$ccc[$v['c']]++;
}else{
$ccc['no_val']++;
}
}

if($ccc['no_val']){
unset($ccc['no_val']);
$ccc['no_val']=1;
}

$err['c']=$ccc;
}

echo json_encode($err);
break;
case 'get_pro_all_list'://获取商品数据
    $user=is_login();
    if(!$user){err('no_login');}
    $shop=get_shop();
    $err['shop']=$shop;
    $err['set']=$set;
    $sql='select id,img,title,price,old,total,c,s,daiwei,state,info,is_new,is_hot from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" ';
    $c=g('c');
    if($c){
        if($c=='no_val'){
            $sql.=' and c is Null ';
        }else{

            $sql.=' and c=:c';$p['c']=$c;}}
    $key=g('key');
    if($key){$sql.=' and temp like "%'.a($key).'%"';}
    $page=g('page');
    if(!$page){$page=1;}$page_num=5000;
    $start=($page-1)*$page_num;
    $sql.=' order by id desc limit '.$start.','.$page_num;
    $l=db($sql,$p);
    if($l){
        foreach($l as $v){
            if($v['img']){
                $v['im']=$v['img'];
                $v['img']=get_img($v['img'],'_240');}
            $v['title']=base64($v['title']);
            $v['info']=base64($v['info']);

            $list['item_'.$v['id']]=$v;
        }

        $err['list']=$list;
    }

    $sql='select c from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" group by c order by id desc';
    $cc=db($sql);
    if($cc){
        foreach($cc as $v){
            if($v['c']){
                $ccc[$v['c']]++;
            }else{
                $ccc['no_val']++;
            }
        }

        if($ccc['no_val']){
            unset($ccc['no_val']);
            $ccc['no_val']=1;
        }

        $err['c']=$ccc;
    }

    echo json_encode($err);
    break;

case 'get_reload_pro'://获取商品
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定商品');}
$sql='select * from '.$cfg['e'].'pro where id=:id ';$p['id']=$id;
$l=db1($sql,$p);
if(!$l){err('没有找到商品信息');}
$l['im']=$l['img'];
$l['img']=get_img($l['img'],'_240');
$l['title']=base64($l['title']);
$l['info']=base64($l['info']);
$err['pro']=$l;
echo json_encode($err);
break;


case 'pro_del'://删除商品
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定需要删除的商品');}
$sql='select * from '.$cfg['e'].'pro where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到商品，或者您没有删除的权限');}
$sql='delete from '.$cfg['e'].'pro where id="'.$l['id'].'"';
sql_query($sql);
err('ok');

break;




case 'quick_add_pro'://添加商品
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');
if(!$data){err('请指定商品图片');}
$data=json_decode($data,1);if(!$data){err('商品错误');}
$arr['shop_id']=$shop['id'];
$arr['temp']='新商品';
$arr['title']=base64_encode($arr['temp']);
$arr['date']=time();
$arr['user_id']=$user['id'];
$arr['total']=-1;
$arr['state']=1;
foreach($data as $v){
$arr['img']=$v['img'];
$num++;
insert($cfg['e'].'pro',$arr);
}

$sql='select id,img,title,price,old,total,c,s,daiwei from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'" order by id desc limit '.$num;
$l=db($sql);
if($l){
foreach($l as $v){
if($v['img']){$v['img']=get_img($v['img'],'_240');}
$v['title']=base64($v['title']);
$list['item_'.$v['id']]=$v;
}

$err['list']=$list;
}

echo json_encode($err);


break;

case 'hd_save_pro'://活动保存商品
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);if(!$l){err('抱歉，没有找到活动，或您没有编辑的权限');}
$data=p('data');
if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}

if($l['pro']){
$pro=unserialize(out($l['pro']));
}

foreach($data as $k=>$v){
$pro[$k]=$v;
}

//print_r($pro);

$arr['pro']=serialize($pro);

update($cfg['e'].'hd',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'get_pro_edit':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$err['set']=$set;
if(!$id){err('请指定商品');}
$sql='select * from '.$cfg['e'].'pro where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}
$l=db1($sql);
if(!$l){err('没有找到商品，或者您没有编辑的权限 ');}
if($l['img']){
$l['im']=$l['img'];
$l['img']=get_imgs($l['img']);
}else{$l['img'][0]=$cfg['no_img'];}
$l['title']=base64($l['title']);
if($l['data']){$l['data']=unserialize(out($l['data']));}
if($l['html']){$l['html']=unserialize(base64($l['html']));}
if($l['info']){$l['info']=base64($l['info']);}
$err['list']=$l;
$err['no_img']=$cfg['no_img'];


$sql='select c from '.$cfg['e'].'pro where shop_id="'.$shop['id'].'" and c IS NOT NULL group by c';
$c=db($sql);
$err['c']=$c;



echo json_encode($err);
break;



case 'pro_data_edit'://商品参数添加
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$id=g('id');if(!$id){err('请指定商品');}

$sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}

$l=db1($sql);if(!$l){err('没有找到商品，或者您没有编辑的权限');}

$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data or !is_array($data)){err('数据错误');}
$k=g('k');if(!$k){$k=0;}

$c=g('c');if($c!='edit'){$c='add';}
if($l['data']){$d=unserialize(out($l['data']));}else{$d=[];}
if(!$data['c']){err('请输入分类');}
if($c=='add'){
if($data['num']!=-1){if($data['num']<-1){$data['num']=0;}}
if($d[$data['c']]){//添加
//print_r($data);
if(!$data['c']){err('请输入标题');}
if(!$data['price']){err('请输入商品的价格');}

$d[$data['c']]['item'][]=array('title'=>$data['c'],'price'=>$data['price'],'num'=>$data['num']);
}else{//新增分类
$d[$data['c']]=array('price'=>$data['price'],'num'=>$data['num'],'title'=>$data['c'],'xiangou'=>$data['xiangou']);
}

}else{//编辑
if(!$data['c']){err('分类不能为空');}
foreach($data as $m=>$n){
if($m!='c'){
if(!$d[$data['c']]['item'] or $k=='main'){
$d[$data['c']][$m]=$n;
}else{
$d[$data['c']]['item'][$k][$m]=$n;}
}
}
}

$num=pro_num($d);
if($num==0 and $l['total']!=-1){$arr['total']=0;}else{
$arr['total']=$num;
}

$arr['data']=serialize($d);
update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'pro_data_del'://删除商品参数
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}

$l=db1($sql);if(!$l){err('没有找到商品，或者您没有编辑的权限');}
if(!$l['data']){err('ok');}
$data=unserialize(out($l['data']));
$k=g('k');if(!$k){$k=0;}$c=g('c');if(!$c){err('参数错误');}
if(!is_array($data)){err('数据错误');}
unset($data[$c]['item'][$k]);
if(!$data[$c]['item'] and !$data[$c]['price']){unset($data[$c]);}
if($data){$arr['data']=serialize($data);}else{$arr['data']='';}
$num=pro_num($data);
if($num==0 and $l['total']!=-1){$arr['total']=0;}else{
$arr['total']=$num;
}
update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'pro_c_del'://删除整个分类
$id=g('id');if(!$id){err('请指定商品');}
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}

$l=db1($sql);if(!$l){err('没有找到商品，或者您没有编辑的权限');}
if(!$l['data']){err('ok');}
$data=unserialize(out($l['data']));if(!$data){err('ok');}
$k=g('k');if(!$k){err('请指定分类');}
unset($data[$k]);
if($data){$arr['data']=serialize($data);}else{$arr['data']='';}
$num=pro_num($data);
if($num==0 and $l['total']!=-1){$arr['total']=0;}else{
$arr['total']=$num;
}
update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'pro_data_save'://保存商品
$id=g('id');if(!$id){err('请指定商品');}
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}
$l=db1($sql);if(!$l){err('没有找到商品，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['img','title','daiwei','info','html','c'];
$cc=['price','old','total','is_new','is_hot','state','xiangou'];
foreach($c as $v){
if($data[$v]){
if($data[$v]=='null'){$data[$v]='';}
$arr[$v]=$data[$v];$can++;
}
}

foreach($cc as $v){
if(is_numeric($data[$v])){
$arr[$v]=$data[$v];$can++;
}
}

if($arr['price']<0){$arr['price']=0;}
if($arr['old']<0){$arr['old']=0;}
if($arr['total']<-1){$arr['total']=-1;}
if($arr['xiangou']<-1){$arr['xiangou']=-1;}
if($arr['title']){$arr['title']=base64_encode($arr['title']);$arr['temp']=$arr['title'];}

if($arr['info']){$arr['info']=base64_encode($arr['info']);}
if($arr['html']){$arr['html']=base64_encode(serialize($arr['html']));}

if(!$arr){err('数据错误');}
if($can){
update($cfg['e'].'pro',$arr,' where id="'.$l['id'].'"');
}
echo json_encode($err);
break;



case 'html_edit':
if(!$id){err('请指定商品');}
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$c=g('c');if(!$id){err('请指定需要获取的介绍');}


switch($c){
default:err('参数错误');break;
case 'pro'://商品
$sql='select * from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'"';
if($user['is_emp']){$sql.=' and user_id="'.a($user['id']).'" ';}
$l=db1($sql);if(!$l){err('没有找到商品信息');}
break;

}

$data=$_POST['data'];
if(!$data){$arr['html']='';}else{
$html=json_decode($data,true);$arr['html']=base64_encode(serialize($html));
}
update($cfg['e'].$c,$arr,' where id="'.a($l['id']).'" ');
sql_error();
err('ok');
break;



case 'get_html':
if(!$id){err('请指定需要获取的介绍');}
$id=g('id');if(!$id){err('请指定商品');}
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$c=g('c');

switch($c){
default:err('参数错误');break;
case 'pro'://商品
$sql='select id,html from '.$cfg['e'].'pro where id="'.a($id).'" and shop_id="'.$shop['id'].'"';
break;

}

$l=db1($sql);if(!$l){err('没有找到相关内容');}
if(!$l['html']){
$html['item_0']=array('value'=>'','type'=>'text');
}else{
$html=unserialize(base64($l['html']));
}
$err['html']=$html;
echo json_encode($err);
break;


case 'get_yun_list':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'yun where shop_id="'.$shop['id'].'" ';
$page=g('page');if(!$page){$page=1;}
$page_num=20;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$list['item_'.$v['id']]=$v;
}
$err['list']=$list;

}
$err['set']=$set;
echo json_encode($err);
break;


case 'yun_creat'://创建运费模板
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('参数错误');}
$arr['title']=$data['title'];
if(!$arr['title']){err('请输入运费模板名称');}
$arr['shop_id']=$shop['id'];
$arr['user_id']=$user['id'];
$arr['is_zt']=1;
$arr['date']=time();
$arr['is_ps']=1;
$arr['city']=$shop['dist'];
$id=insert($cfg['e'].'yun',$arr);
$err['id']=$id;
echo json_encode($err);
break;


case 'get_yun_edit':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定运费模板');}
$sql='select * from '.$cfg['e'].'yun where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到运费模板，或者您没有编辑的权限');}
if($l['zt_addr']){$l['ztd']=get_zt_addr($l['zt_addr']);}
$err['list']=$l;
$err['set']=$set;
echo json_encode($err);
break;

case 'yun_save':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定运费模板');}
$sql='select * from '.$cfg['e'].'yun where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('没有找到运费模板，或者您没有编辑的权限');}
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}

$c=['title','city','kd_data','info','zt_addr'];
$n=['is_zt','is_ps','is_kd','ps_num','ps_price','ps_yun','ps_baoyou','ps_pre_price','kd_price','kd_baoyou','kd_pre_price','kd_min_num','ps_max','ps_min'];
foreach($c as $v){
if($data[$v]){$can++;
if($data[$v]=='null'){$data[$v]='';}
$arr[$v]=$data[$v];
}
}
foreach($n as $v){
if($data[$v] or is_numeric($data[$v])){$can++;
if($data[$v]=='null'){$data[$v]='0';}
$arr[$v]=$data[$v];
}
}
//print_r($arr);
if(!$can){exit;}
//if($arr['zt_addr']){$arr['zt_addr']=serialize($arr['zt_addr']);}
update($cfg['e'].'yun',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'get_addr':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$sql='select * from '.$cfg['e'].'addr where shop_id="'.$shop['id'].'" order by id desc';
$l=db($sql);
if($l){
foreach($l as $v){
$list['item_'.$v['id']]=$v;
}

$err['list']=$list;
}
$err['set']=$set;
echo json_encode($err);


break;

case 'addr_edit':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$cc=['title','pro','city','dist','addr','name','phone','latitude','longitude'];
foreach($cc as $v){
if($data[$v]){$arr[$v]=$data[$v];}
}

if($arr['addr']){
$arr['addr']=str_replace($data['pro'],'',$arr['addr']);
$arr['addr']=str_replace($data['city'],'',$arr['addr']);
$arr['addr']=str_replace($data['dist'],'',$arr['addr']);
}


if(!$arr){err('数据错误');}
if($id){
$sql='select * from '.$cfg['e'].'addr where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);if(!$l){err('没有找到自提点，或者您没有编辑的权限');}
update($cfg['e'].'addr',$arr,' where id="'.$l['id'].'"');

}else{
$arr['date']=time();
$arr['user_id']=$user['id'];
$arr['shop_id']=$shop['id'];
$id=insert($cfg['e'].'addr',$arr);
$err['id']=$id;
}
echo json_encode($err);


break;

case 'addr_del':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定需要删除的自提点');}
$sql='select * from '.$cfg['e'].'addr where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);if(!$l){err('没有找到自提点，或者您没有删除的权限');}
$sql='delete from '.$cfg['e'].'addr where id="'.$l['id'].'"';
sql_query($sql);
err('ok');
break;

case 'yun_del'://删除运费模板
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定需要删除的运费模板');}
$sql='select * from '.$cfg['e'].'yun where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);if(!$l){err('没有找到运费模板，或者您没有删除的权限');}
$sql='delete from '.$cfg['e'].'yun where id="'.$l['id'].'"';
sql_query($sql);
err('ok');

break;


case 'yun_select'://选择运费
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定数据');}
$c=g('c');if(!$c){err('参数错误');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$arr['yun_id']=$data['yun_id'];
$arr['yun_title']=$data['yun_title'];
$sql='select * from '.$cfg['e'].$c.' where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);
if(!$l){err('没有找到数据，或者您没有编辑的权限');}
if(!$arr['yun_id']){err('没有设置运费模板');}
update($cfg['e'].$c,$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'fen_edit'://加粉
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);
if(!$data){err('数据错误');}
$arr['data']=base64_encode(serialize($data));
if($id){
$sql='select * from '.$cfg['e'].'data where c="fen" and id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'" ';}
$l=db1($sql);
if(!$l){err('抱歉，没有找到数据，或者您没有编辑的权限');}
update($cfg['e'].'data',$arr,' where id="'.$l['id'].'"');
err('ok');
}

$arr['c']='fen';$arr['user_id']=$user['id'];$arr['shop_id']=$shop['id'];
$id=insert($cfg['e'].'data',$arr);
$err['id']=$id;
echo json_encode($err);
break;


case 'get_fen':
$user=is_login();
if(!$user){err('no_login');}
$err['set']=$set;
$shop=get_shop();$err['user']=$user;
$sql='select * from '.$cfg['e'].'data where c="fen" and shop_id="'.$shop['id'].'" order by id desc';

$l=db($sql);
if($l){
foreach($l as $v){
if($v['data']){$v['data']=unserialize(base64($v['data']));}
$list['item_'.$v['id']]=$v;
}
$err['list']=$list;
}

echo json_encode($err);
break;

case 'del_data':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定需要删除的数据');}
$sql='select * from '.$cfg['e'].'data where id="'.$id.'" and shop_id="'.$shop['id'].'"';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);if(!$id){err('没有找到数据，或者您没有删除的权限');}
$sql='delete from '.$cfg['e'].'data where id="'.$l['id'].'"';
sql_query($sql);
err('ok');
break;


case 'get_nuo':
$user=is_login();
if(!$user){err('no_login');}
$err['set']=$set;
$shop=get_shop();$err['user']=$user;
$sql='select * from '.$cfg['e'].'data where c="nuo" and shop_id="'.$shop['id'].'" order by id desc';

$l=db($sql);
if($l){
foreach($l as $v){
if($v['data']){$v['data']=base64($v['data']);}
$list['item_'.$v['id']]=$v;
}
$err['list']=$list;
}

echo json_encode($err);

break;

case 'nuo_save':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$arr['data']=g('val');
if(!$arr['data']){err('请输入承诺内容');}
$arr['data']=base64_encode($arr['data']);
if($id){
$sql='select * from '.$cfg['e'].'data where id="'.$id.'" and shop_id="'.$shop['id'].'"';
if($user['is_emp']){$sql.=' and user_id="'.$user['id'].'"';}
$l=db1($sql);if(!$id){err('没有找到数据，或您没有编辑的权限');}
update($cfg['e'].'data',$arr,' where id="'.$l['id'].'"');
err('ok');
}

$arr['c']='nuo';$arr['user_id']=$user['id'];$arr['shop_id']=$shop['id'];
$id=insert($cfg['e'].'data',$arr);
$err['id']=$id;
echo json_encode($err);
break;


case 'get_shop'://获取门店信息
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
if($user['shop_id']){
$shop_id=$user['shop_id'];
}else{
err('no_shop');
//$shop_id=creat_shop();
}
if(!$shop_id){err('no_shop');}
$sql='select * from '.$cfg['e'].'shop where id="'.$shop_id.'" ';
$shop=db1($sql);
$shop['shop_name']=base64($shop['shop_name']);
$err['shop']=$shop;

if(!$err['shop']['bg']){
$err['shop']['bg'][]=$set['app_bg'][0];
}else{
$err['shop']['bg']=unserialize(out($err['shop']['bg']));
}
$err['set']=$set;
echo json_encode($err);
break;

case 'shop_edit':
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
$shop=get_shop();
if(!$user['shop_id']){
if($user['is_emp']){err('抱歉，您没有编辑门店的权限');}}
$data=p('data');if(!$data){err('参数错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['shop_name','name','phone','latitude','longitude','pro','city','dist','addr','logo','bg'];
foreach($c as $v){
if($data[$v]){
$arr[$v]=$data[$v];
}
}
if(!$arr){err('保存数据错误');}
if($arr['shop_name']){
$arr['shop_name']=base64_encode($arr['shop_name']);
}
if($arr['bg']){
$arr['bg']=serialize($arr['bg']);
}

if($arr['addr'] and $arr['pro']){
$re=[$arr['pro'],$arr['city'],$arr['dist']];
$arr['addr']=str_replace($re,'',$arr['addr']);
}


update($cfg['e'].'shop',$arr,' where id="'.$user['shop_id'].'"');
err('ok');

break;

case 'creat_shop'://创建门店
$user=is_login();
if(!$user){err('no_login');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
if(!$data['shop_name']){err('请输入门店名称');}
if(!$data['pro']){err('请选择门店区域');}
if(!$data['addr']){err('请输入门店的地址');}
if(!$data['latitude']){err('请选择门店位置');}
if(!$data['phone']){err('请输入联系电话');}
if(!$data['logo']){err('请选择上传一个门店的logo');}
if(!$data['name']){err('请输入联系人');}
$sql='select * from '.$cfg['e'].'shop where user_id="'.$user['id'].'" ';
$ck=db1($sql);
if($ck){err('抱歉，您已经存在门店');}

$arr['shop_name']=base64($data['shop_name'],1);$arr['name']=$data['name'];if(!$arr['name']){$arr['name']='';}
$arr['pro']=$data['pro'];$arr['city']=$data['city'];$arr['dist']=$data['dist'];
$arr['addr']=str_replace([$arr['pro'],$arr['city'],$arr['dist']],'',$data['addr']);$arr['latitude']=$data['latitude'];$arr['longitude']=$data['longitude'];
$arr['phone']=$data['phone'];$arr['logo']=$data['logo'];
$arr['adm_id']=$user['id'];$arr['user_id']=$user['id'];
$arr['state']=1;$arr['is_agree']=1;$arr['code']='gz:'.md5(time());
$arr['date']=time();
$id=insert($cfg['e'].'shop',$arr);
if($id){
$user_arr['shop_id']=$id;$user_arr['shop_state']=1;
update($cfg['e'].'users',$user_arr,' where id="'.$user['id'].'"');
r_set($cfg['path'].'user_'.$user['id'],'',600);
}
err('ok');
break;



case 'order_set_yun'://订单设置运费
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');if(!$id){err('请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.a($id).'" and user_id="'.$user['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到订单');}
if($l['state']>0){err('抱歉，您的订单已经支付,无法进行修改');}
//查找运费模板
$sql='select * from '.$cfg['e'].'yun where id="'.$l['yun_id'].'" ';
$yun=db1($sql);if(!$yun){err('没有找到运费模板');}

$arr['yun']=g('val');$arr['dis']=0;
switch($arr['yun']){
case 'is_zt':
if(!$yun['is_zt']){err('抱歉，该门店无法自提');}break;
case 'is_ps'://配送
if(!$yun['is_ps']){err('抱歉，该门店不支持配送,请联系门店');}break;
case 'is_kd'://快递
if(!$yun['is_kd']){err('抱歉，该门店不支付快递到家，如有问题，请联系门店');}break;
}
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
$arrs['my_dft_yun']=$arr['yun'];
update($cfg['e'].'users',$arrs,'where id="'.$user['id'].'"');
sql_error();
echo json_encode($err);
break;


case 'order_user_change'://订单更换信息
$user=is_login();
if(!$user){err('no_login');}
$id=g('id');if(!$id){err('请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.a($id).'"  and user_id="'.$user['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到订单');}
if($l['state']>0){err('抱歉，您的订单已经支付,无法进行修改');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('请输入需要保存的数据');}
$a=['name','phone','pro','city','dist','addr','msg','latitude','longitude','zt_addr'];//可以修改的字符
foreach($data as $k=>$v){
if(in_array($k,$a)){$arr[$k]=$v;}
}
if(!$arr){err('数据错误');}
if($arr['latitude'] or $arr['longitude']){//重新定位后，距离更新
$arr['dis']=0;
}

if($arr['dist'] or $arr['addr']){
if($arr['dist']!=$l['dist'] or $arr['addr']!=$l['addr']){
//地址更新，更新位置信息
$arr['dis']=0;//$arr['latitude']=0;$arr['longitude']=0;
}}

update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');sql_error();
unset($arr['msg'],$arr['dis']);
if($arr){
update($cfg['e'].'users',$arr,' where id="'.$user['id'].'"');sql_error();
}
err('ok');
break;


case 'gz_shop':
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定门店');}
$arr['need_post']=g('val');
if($arr['need_post']!=1){$arr['need_post']=0;}
update($cfg['e'].'shop_gz',$arr,' where shop_id="'.$id.'" and user_id="'.$user['id'].'"');
err('ok');
break;

case 'get_my_info'://获取我的信息
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
$err['info']['warn']='您可以更新头像和昵称。';
$err['set']=$set;
echo json_encode($err);

break;
case 'get_my_shop'://获取我的门店信息
$user=is_login();
if(!$user){err('no_login');}
$err['shop']=get_my_shop();

$err['info']['warn']='为了您的隐私，您可以更换头像及昵称。';
$err['set']=$set;
echo json_encode($err);
break;


case 'my'://我的后台中心
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
if($user['shop_id']){
$shop_id=$user['shop_id'];
}else{
if($user['is_emp'] and $user['emp_state']){
$shop_id=$user['is_emp'];
}
}


if($shop_id){$shop=get_hd_shop($shop_id);}

$err['set']=$set;
//$err['icon']['我的']['我的订单']=array('icon'=>'icon-dingdan1','url'=>'/pages/my/order');
$err['icon']['我的']['我参与的团购']=array('icon'=>'icon-_xuanxiang','url'=>'/pages/order/index');

$err['icon']['我的']['修改信息']=array('icon'=>'icon-wode','url'=>'/pages/my/edit');
$err['icon']['我的']['我的关注']=array('icon'=>'icon-pintuan','url'=>'/pages/my/gz');
$err['icon']['我的']['管理收货地址']=array('icon'=>'icon-wuliu','ac'=>'get_addr');

//$err['icon']['我的']['优惠券']=array('icon'=>'icon-youhuiquan','url'=>'/pages/my/quan');
//$err['icon']['我的']['收藏夹']=array('icon'=>'icon-like','url'=>'/pages/my/fav');
$err['icon']['我的']['关于我们']=array('icon'=>'icon-wode2','url'=>'/pages/help/index?c=关于我们');
$err['icon']['我的']['投诉建议']=array('icon'=>'icon-chat_bubble_text','url'=>'/pages/my/ts_jy');
$err['icon']['我的']['退出登录']=array('icon'=>'icon-mb_zhuxiao','ac'=>'loginout','style'=>'color:crimson;');

$err['info']['bg']=$set['bg'][0];

if($user['is_emp']>0 or $user['shop_id']>0){
if($user['shop_state']>0 or $user['emp_state']>0){//门店
$err['index']['订单核销']=array('icon'=>'icon-saomiao','ac'=>'hexiao','style'=>'color:crimson;');
$order_num=0;
$sql='select count(id) as num from '.$cfg['e'].'order where shop_id="'.$shop['id'].'" and state=1 ';
$oo=db1($sql);
if($oo){$order_num=$oo['num'];}
$err['index']['管理订单']=array('icon'=>'icon-order','url'=>'/pages/adm/order','num'=>$order_num);

$err['index']['活动列表']=array('icon'=>'icon-dianpu','url'=>'/pages/adm/hd_list');
$err['index']['商品管理']=array('icon'=>'icon-chaxunrenwu','url'=>'/pages/pro/index');


if($user['shop_id']>0){
$shop=get_my_shop();
if($shop['state']==1){
$err['index']['管理门店']=array('icon'=>'icon-dianpu','url'=>'/pages/shop/index');
$err['index']['资金管理']=array('icon'=>'icon-jine','url'=>'/pages/money/index');
$err['index']['雇员管理']=array('icon'=>'icon-xuanzhonghaoyou','url'=>'/pages/adm/emp');
$err['index']['团长管理']=array('icon'=>'icon-friendfavor','url'=>'/pages/adm/tz');
}else{

$err['index']['创建门店']=array('icon'=>'icon-dianpu','url'=>'/pages/shop/creat');

}
}
$err['index']['PC站点登录']=array('icon'=>'icon-qrcode','ac'=>'scan_login');
}
}


/*
//门店管理权限
if($user['is_emp']>0 or $user['shop_id']>0){//如果是雇员

if($user['shop_state']>0 or $user['emp_state']>0){
$err['icon']['商城管理']['订单核销']=array('icon'=>'icon-saomiao','ac'=>'hexiao','style'=>'color:crimson;');

$err['icon']['商城管理']['管理订单']=array('icon'=>'icon-order','url'=>'/pages/adm/order');

$sql='select count(id) as num from '.$cfg['e'].'order where shop_id="'.$shop['id'].'" and state in (80,88)';
$oo=db1($sql);
if($oo){
$err['icon']['商城管理']['待处理订单']=array('icon'=>'icon-order','url'=>'/pages/adm/order','num'=>$oo['num']);
}
$err['icon']['商城管理']['活动列表']=array('icon'=>'icon-dianpu','url'=>'/pages/adm/hd_list');
$err['icon']['商城管理']['商品管理']=array('icon'=>'icon-chaxunrenwu','url'=>'/pages/pro/index');
}

if($user['shop_id']>0){
if($user['shop_state']==1){
$err['icon']['商城管理']['管理门店']=array('icon'=>'icon-dianpu','url'=>'/pages/shop/index');
}else{
$err['icon']['商城管理']['管理门店(已关闭)']=array('icon'=>'icon-dianpu','url'=>'/pages/shop/index');
}

$err['icon']['商城管理']['资金管理']=array('icon'=>'icon-jine','url'=>'/pages/money/index');
$err['icon']['商城管理']['雇员管理']=array('icon'=>'icon-xuanzhonghaoyou','url'=>'/pages/adm/emp');
$err['icon']['商城管理']['团长管理']=array('icon'=>'icon-friendfavor','url'=>'/pages/adm/tz');
}

$err['icon']['商城管理']['PC站点登录']=array('icon'=>'icon-saomiao','ac'=>'scan_login');
}
*/
echo json_encode($err);
break;


case 'get_tui_order'://退款订单
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定需要退款的订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" ';
$l=db1($sql);if(!$l){err('没有找到退款订单');}
if($l['state']==10){err('抱歉，您的订单已经签收，无法退款，您可以进行申请售后服务。');}
if($l['state']==0){err('抱歉，您的订单还没有支付');}
if($l['state']>10 or $l['out_refund_no']){err('抱歉，该订单已经申请退款。');}

$l['text']='申请退款';
if($set['tui_time']>0){
$time=$l['pay_date']+$set['tui_time']*3600;
if($time>time()){
$l['text']='急速退款';
}
}

$l['pay_date']=date('Y-m-d H:i:s',$l['pay_date']);
$l['pay_price']=number_format($l['pay_price']/100,2,'.','');


$err['list']=$l;
echo json_encode($err);
break;

case 'get_shouhou_order'://获取售后订单
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定需要售后的订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" ';
$l=db1($sql);if(!$l){err('没有找到退款订单');}
if($l['state']<10){err('抱歉，您的订单没有完成，请完成后申请售后。');}
if($l['state']==0){err('抱歉，您的订单还没有支付');}
if($l['state']>10 or $l['out_refund_no']){err('抱歉，该订单已经申请退款。');}

$l['text']='售后申请';


$l['pay_date']=date('Y-m-d H:i:s',$v['finish_date']);
$l['pay_price']=number_format($l['pay_price']/100,2,'.','');


$err['list']=$l;
echo json_encode($err);

break;
case 'shouhou_submit'://售后申请提交
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定需要退款的订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" ';
$l=db1($sql);if(!$l){err('没有找到退款订单');}
if($l['state']<10){err('抱歉，您的订单没有完成，请完成后再申请。');}
if($l['state']==0){err('抱歉，您的订单还没有支付');}
if($l['state']>10 or $l['out_refund_no']){err('抱歉，该订单已经申请退款或者售后，如遇问题，请联系我们。');}
$data=p('data');if($data){$data=json_decode($data,1);}
if($data['tui_msg']){$arr['tui_msg']=$data['tui_msg'];}
if($data['tui_data']){$arr['tui_data']=serialize($data['tui_data']);}
$arr['state']=80;
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');

//发送消息给商户
$shop=get_hd_shop($l['shop_id']);
if($shop['wx_name']){

}

err('ok');
break;

case 'tuikuan'://对订单进行退款
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定需要退款的订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" ';
$l=db1($sql);if(!$l){err('没有找到退款订单');}
if($l['state']==10){err('抱歉，您的订单已经签收，无法退款，您可以进行申请售后服务。');}
if($l['state']==0){err('抱歉，您的订单还没有支付');}
if($l['state']>10 or $l['out_refund_no']){err('抱歉，该订单已经申请退款。');}
$data=p('data');if($data){$data=json_decode($data,1);}
if($data['tui_msg']){$arr['tui_msg']=$data['tui_msg'];}
if($data['tui_data']){$arr['tui_data']=serialize($data['tui_data']);}

$can_tui=1;
if($set['tui_time']){
$time=$l['pay_date']+$set['tui_time']*3600;
if($time<time()){$can_tui=0;}
}



if($can_tui){//可以进行退款
$arr['out_refund_no']=time();
$arr['tui_pirce']=$l['pay_price'];
$arr['tui_date']=time();
$set=get_set(1);
$re=tuikuan($l,$arr);

if($re=='ok'){
$arr['state']=99;
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
$shop=get_hd_shop($l['shop_id']);

//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['order_id']=$l['id'];
$zd['nickname']=$l['nickname'];
$zd['logo']=$l['logo'];
//管理员账单
$zd['user_id']=$shop['adm_id'];
$zd['title']='订单退款';
$zd['c']='退款';
$zd['money']=-number_format($l['pay_price']/100,2,'.','');//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);

//发送消息给商户
if($shop['wx_name']){
tui_msg($order,'用户主动退款');
}
err('ok');
}


}else{//手动退款
$arr['state']=88;
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
err('您的退款提交成功，我们将尽快为您退款，亲注意查收。');
}

break;

case 'adm_tuikuan'://管理员强制退款给用户
$user=is_login(1);
if(!$user){err('no_login');}
$set=get_set(1);
$shop=get_shop();
if(!$id){err('请指定需要退款的订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and shop_id="'.$shop['id'].'" and state in (80,88)';
$l=db1($sql);if(!$l){err('没有找到退款订单');}
$arr['out_refund_no']=time();
$arr['tui_pirce']=$l['pay_price'];
$arr['tui_date']=time();
$re=tuikuan($l,$arr);

if($re=='ok'){
$arr['state']=99;
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
$shop=get_hd_shop($l['shop_id']);

//账单
$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['order_id']=$l['id'];
$zd['nickname']=$l['nickname'];
$zd['logo']=$l['logo'];
//管理员账单
$zd['user_id']=$shop['adm_id'];
$zd['title']='订单退款(管理端)';
$zd['c']='退款';
$zd['money']=-number_format($l['pay_price']/100,2,'.','');//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);

//发送消息给商户
if($shop['wx_name']){
tui_msg($order,'用户主动退款');
}
err('ok');
}

break;


case 'get_gz_shops'://获取我的关注
$user=is_login();
if(!$user){err('no_login');}
$c=g('c');
if($c=='adm'){
$shop=get_shop();
$sql='select * from '.$cfg['e'].'shop_gz where shop_id="'.$shop['id'].'" ';
}else{
$sql='select * from '.$cfg['e'].'shop_gz where user_id="'.$user['id'].'" ';
}

$key=g('key');
if($key){$sql.=' and shop_name like "%'.a($key).'%" ';}

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=20;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;

$l=db($sql);
if($l){
foreach($l as $v){
if($c=='adm'){
$v['user']=get_user($v['user_id']);
}else{
$v['shop']=get_hd_shop($v['shop_id']);
}


$list['item_'.$shop['id']]=$v;
}

$err['list']=$list;
}

echo json_encode($err);
break;
case 'gz_del'://删除关注
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定关注内容');}
$c=g('c');
if($c=='adm'){
$shop=get_shop();
$sql='select * from '.$cfg['e'].'shop_gz where id="'.$id.'" and shop_id="'.$shop['id'].'"';
}else{
$sql='select * from '.$cfg['e'].'shop_gz where id="'.$id.'" and user_id="'.$user['id'].'"';
}

$l=db1($sql);
if(!$l){err('没有找到内容，或者已经删除');}
$sql='delete from '.$cfg['e'].'shop_gz where id="'.$l['id'].'"';
sql_query($sql);
$sql='update '.$cfg['e'].'shop set people_num=people_num-1 where id="'.$l['shop_id'].'"';
sql_query($sql);
$sql='update '.$cfg['e'].'users set shop_gz_num=shop_gz_num-1 where id="'.$l['user_id'].'"';
sql_query($sql);
err('ok');
break;

case 'hexiao'://订单核销
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$val=g('val');if(!$val){err('核销码错误');}
$str=substr($val,0,7);
if($str!='hexiao:'){err('核销码错误');}
$code=str_replace('hexiao:','',$val);
$sql='select * from '.$cfg['e'].'order where code=:code and shop_id="'.$shop['id'].'" ';
$p['code']=$code;
$l=db1($sql,$p);
if(!$l){err('抱歉，没有找到订单');}
if($l['state']==0){err('抱歉，该订单没有支付');}
if($l['state']==10){err('该订单已经完成');}
if($l['state']>10){err('抱歉，该订单存在退款或者售后问题，核销失败');}
$shop=get_hd_shop($l['shop_id']);
$re=order_jiesuan($l);
if($re=='ok'){
$arr['state']=10;$arr['finish_date']=time();

//cps
//活动
$sql='select * from '.$cfg['e'].'hd where id="'.$l['hd_id'].'" ';
$hd=db1($sql);

//团长
if($l['from_id']>0 and $l['cps_bl']>0 and $hd['cps_pay']==2 and $l['cps_jiesuan']==0){//自动支付cps时候，进行cps导出

$cps=$l['pro_price']*$l['cps_bl'];
$cps=number_format($cps,2,'.','');
$zd['date']=time();
$zd['order_id']=$l['id'];
$zd['shop_id']=$l['shop_id'];
$zd['title']='订单佣金';
$zd['user_id']=$l['from_id'];
$zd['c']='cps';
$zd['money']=-$cps;
$arr['cps_jiesuan']=time();
$zd['nickname']=$tz['nickname'];
$zd['logo']=$tz['logo'];
insert($cfg['e'].'pay',$zd);
}
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
zd_balance($shop);//账单结算
}else{err($re);}
err('ok');

break;


case 'scan_login'://二维码登录
$user=is_login();
if(!$user){err('no_login');}
$code=g('val');
if(!$code){err('请扫描登录二维码');}
$sql='select * from '.$cfg['e'].'qr where code=:code';
$p['code']=$code;
$l=db1($sql,$p);if(!$l){err('没有找到登录码');}
$time=$l['date']+600;
if($time<time()){err('抱歉，您的二维码已经过期，请重新刷新');}
$arr['user_id']=$user['id'];
update($cfg['e'].'qr',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'get_adm_hd'://获取门店活动列表
$user=is_login();
if(!$user){err('login');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'hd where shop_id="'.$shop['id'].'" ';
$key=g('key');
if($key){$sql.=' and temp like "%'.a($key).'%" ';}
$state=g('state');
if($state){
if($state==99){$state=0;}else{$state=1;}
$sql.=' and state=:state ';
$p['state']=$state;
}

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=20;$start=($page-1)*$page_num;

$sql.=' order by id desc limit '.$start.','.$page_num;

$l=db($sql,$p);
if($l){
foreach($l as $k=>$v){
$v['title']=base64($v['title']);
if($v['img']){$v['img']=get_img($v['img'],'_480');}else{$v['img']=$cfg['no_img'];}
$v['start']=date('m-d H:i',$v['start']);
$v['end']=date('m-d H:i',$v['end']);
$list['item_'.$v['id']]=$v;

}
$err['list']=$list;
}
$err['set']=$set;
echo json_encode($err);

break;



case 'get_shop_emp':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$err['shop']=$shop;
$err['set']=$set;
if(!$user['shop_id']){err('抱歉，仅管理员才能查看');}
$sql='select * from '.$cfg['e'].'users where is_emp="'.$shop['id'].'" ';
$key=g('key');
if($key){$sql.=' and wx_nickname like "%'.a($key).'%" ';}
$page=g('page');if(!$page){$page=1;}
$page_num=20;$start=($page-1)*$page_num;

$sql.=' order by emp_state asc,id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$v['nickname']=base64($v['nickname']);

$list['item_'.$v['id']]=$v;

}
$err['list']=$list;
}
$err['set']=$set;
echo json_encode($err);
break;


case 'emp_band'://绑定
$user=is_login();
if(!$user){err('no_login');}
if($user['is_emp']>0){err('您已经绑定了门店');}
if(!$id){err('请指定门店');}
$shop=get_hd_shop($id);
if(!$shop){err('没有找到门店');}
$arr['is_emp']=$id;$arr['emp_state']=0;
$arr['hd_num']=0;$arr['order_num']=0;$arr['order_price']=0;
update($cfg['e'].'users',$arr,' where id="'.$user['id'].'"');
err('ok');
break;




case 'emp_state':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if($user['shop_id']==0){err('抱歉，您没有权限');}
if(!$id){err('请指定雇员');}
$sql='select * from '.$cfg['e'].'users where id="'.$id.'" and is_emp="'.$shop['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到雇员信息');}
$arr['emp_state']=g('val');
if($arr['emp_state']!=0){$arr['emp_state']=1;}
update($cfg['e'].'users',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'emp_unbind'://解除绑定
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if($user['shop_id']==0){err('抱歉，您没有权限');}
if(!$id){err('请指定雇员');}
$sql='select * from '.$cfg['e'].'users where id="'.$id.'" and is_emp="'.$shop['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到雇员信息');}
$arr['is_emp']=0;$arr['emp_state']=0;
update($cfg['e'].'users',$arr,' where id="'.$l['id'].'"');
err('ok');
break;

case 'get_map_order':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定当前活动');}

$sql='select * from '.$cfg['e'].'order where shop_id="'.$shop['id'].'" and hd_id="'.$id.'" ';
$sql.=' and yun="is_ps" ';
$sql.=' and (state>0 and state<5) ';
$sql.=' order by id desc';

$latitude=g('latitude');
$longitude=g('longitude');
if(!$latitude){err('抱歉，您的定位信息有误，请确认您已经开启位置信息。');}

$l=db($sql);
if($l){
foreach($l as $k=>$v){
$v['nickname']=base64($v['nickname']);
if($v['latitude']){
$v['dis']=get_dis($v['longitude'],$v['latitude'],$longitude,$latitude,1,1).'米';
}else{$err['err_msg']='有部分客户没有显示位置，无法地图上此显示。';}

$list[$k]=$v;
}

$err['list']=$list;
if($list){$err['err_msg']='共有'.count($list).'个待配送订单';}else{$err['err_msg']='您没有待配送订单';}
}

echo json_encode($err);

break;



case 'get_money_list'://获取资金列表
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$user['shop_id']){err('仅门店管理员才能看到资金列表');}

$sql='select * from '.$cfg['e'].'pay where shop_id="'.$shop['id'].'" ';
$c=g('c');
if($c){$sql.=' and c=:c ';$p['c']=$c;}
$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=100;$start=($page-1)*$page_num;

$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);

if($l){
foreach($l as $v){
$m=date('Y年m月',$v['date']);
$v['date']=date('n月j日 H:i',$v['date']);
if(!$v['logo']){$v['logo']=$shop['logo'];}
$v['nickname']=base64($v['nickname']);
$list[$m][]=$v;
}
$err['list']=$list;
}

if($page==1){
$index=array(''=>'全部','pay'=>'商品收入','结算'=>'订单结算','提现'=>'现金提现','cps'=>'佣金支出','退款'=>'用户退款');
$err['index']=$index;
}
$err['set']=$set;
echo json_encode($err);
break;

case 'get_pay_list'://获取提现进度
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$user['shop_id']){err('仅门店管理员才能看到资金列表');}

$sql='select * from '.$cfg['e'].'money_pay where user_id="'.$user['id'].'" ';

$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=100;$start=($page-1)*$page_num;

$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql,$p);

if($l){
foreach($l as $v){
if($v['c']=='user'){$v['title']='提现到零钱';}else{$v['title']='提现到银行卡';}
$m=date('Y年m月',$v['date']);
$v['date']=date('n月j日 H:i',$v['date']);
$list[$m][]=$v;
}
$err['list']=$list;
}

echo json_encode($err);
break;


case 'get_help'://获取帮助
$sql='select * from '.$cfg['e'].'help ';
if($id){
$sql.=' where id="'.$id.'" ';$get_list=1;
}else{
$tag=g('tag');
if($tag){
$sql.=' where tag like "%'.a($tag).'%" ';

}else{
$c=g('c');
if($c){$sql.=' where c=:c';$p['c']=$c;}
}


}
$sql.=' order by o desc,id desc limit 11 ';
$l=db($sql,$p);
if($l){
$help=$l[0];
$help['html']=str_replace('<img','<img style="width:100%"',$help['html']);
$err['help']=$help;
unset($l[0]);
$err['list']=$l;
}
$err['set']=$set;
echo json_encode($err);

break;


case 'get_login':
    $user=is_login();
    if(!$user){err('no_login');}
$err['set']=$set;
$err['user']=$user;
echo json_encode($err);
break;
case 'tj_submit'://提交建议和投诉
$user=is_login();
if(!$user){err('no_login');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('请输入数据');}
$arr['msg']=$data['msg'];
$arr['img']=$data['img'];
if($arr['img']){$arr['img']=serialize($arr['img']);}
$arr['date']=time();
$arr['user_id']=$user['id'];
$arr['nickname']=base64_encode($user['nickname']);
$arr['logo']=$user['logo'];
$arr['state']=1;
insert($cfg['e'].'ts',$arr);
err('ok');
break;


case 'get_order_detail'://获取订单数据
$user=is_login();
if(!$user){err('no_login');}
$code=g('code');
if($code){
$code=str_replace('hexiao:','',$code);
}
//err($code);
if($id or $code){}else{err('请指定订单');}

$c=g('c');if($c!='adm'){$c='user';}

$sql='select * from '.$cfg['e'].'order  ';
if($id){
$sql.=' where id="'.$id.'"';
}else{
$sql.=' where code =:code';
$p['code']=$code;
}

if($c=='user'){$sql.=' and user_id="'.$user['id'].'" ';}else{
$shop=get_shop();//进行管理和查看
$sql.=' and shop_id="'.$shop['id'].'" ';}

$l=db1($sql,$p);if(!$l){err('没有找到订单信息，或者您没有查看的权限');}

switch($l['state']){
case 0:$list['订单状态']='待付款';break;
case 1:$list['订单状态']='已付款';break;
case 2:$list['订单状态']='已接单';break;
case 5:$list['订单状态']='已配送';break;
case 10:$list['订单状态']='完成';break;
case 80:$list['订单状态']='申请售后';break;
case 88:$list['订单状态']='申请退款';break;
case 99:$list['订单状态']='售后/退款';break;
}

$list['下单时间']=date('Y-m-d H:i:s',$l['date']);
$list['商品数']=$l['num'];
$list['商品金额']='￥'.$l['pro_price'];
$list['订单合计']='￥'.$l['total'];
switch($l['yun']){
case 'is_zt':$list['配送方式']='门店自提';
if($l['zt_addr']){
$sql='select * from '.$cfg['e'].'addr where id="'.$l['zt_addr'].'"';
$zt=db1($sql);
$list['自提点']=$zt['title'];
$list['自提点地址']=$zt['addr'];
$list['联系电话']=$zt['phone'];
}

break;
case 'is_ps':$list['配送方式']='配送到家';
$list['配送地址']=$l['addr'];

break;
case 'is_kd':$list['配送方式']='快递';
if($l['send_comp']){
$list['快递公司']=$l['send_comp'];
$list['快递单号']=$l['send_num'];
}
break;
}

$list['运费']='￥'.$l['yun_price'];

if($l['quan_id'] and $l['quan_price']){
$list['优惠券']=$l['quan_info'];
$list['优惠金额']='￥'.$l['quan_price'];
}



if($l['pay_date']){
$list['支付金额']='￥'.number_format($l['pay_price']/100,2,'.','');
$list['支付时间']=date('Y-m-d H:i:s',$l['pay_date']);
}



if($l['finish_date']){
$list['完成时间']=date('Y-m-d H:i:s',$l['finish_date']);
}

if($l['tui_price']>0){

$list['退款金额']='￥'.number_format($l['tui_price']/100,2,'.','');
$list['退款时间']=date('Y-m-d H:i:s',$l['tui_date']);
}

$list['活动']=base64($l['hd_title']);

if($l['msg']){
$list['订单消息']=$l['msg'];
}

if($c=='adm'){
if($l['from_id']>0 and $l['cps_bl']>0){
$tz['分佣订单']='是';
$tz['预估佣金']='￥'.number_format($l['pro_price']*$l['cps_bl'],2,'.','');
if($l['cps_order_no']){$tz['分佣订单号']=$l['cps_order_no'];}else{
$tz['状态']='未完成分佣';
}
if($l['cps_pay_date']){$tz['分佣时间']=date('Y-m-d H:i:s',$l['cps_pay_date']);}

$tz_user=get_user($l['from_id']);
$tz['团长昵称']=$tz_user['nickname'];


}else{$tz['分佣订单']='否';}

$err['tz']=$tz;
}
if($l['tui_data']){//申请售后
$l['tui_data']=unserialize(out($l['tui_data']));
}

if($l['send_data']){//申请售后
$l['send_data']=unserialize(out($l['send_data']));
}

$u['logo']=$l['logo'];
$u['昵称']=base64($l['nickname']);
$u['联系人']=$l['name'];
$u['联系电话']=$l['phone'];
$u['省市区']=$l['pro'].$l['city'].$l['dist'];
$u['地址']=$l['addr'];

$err['pro']=get_order_pro($l['id']);



$err['user_info']=$u;
$err['list']=$list;
$err['order']=$l;
$err['set']=$set;
echo json_encode($err);
break;

case 'order_ok'://订单完成
$user=is_login(1);if(!$user){err('no_login');}
if(!$id){err('抱歉，请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and user_id="'.$user['id'].'" ';
$l=db1($sql);
if(!$l){err('没有找到订单，或者您没有管理的权限');}
if($l['state']<1){err('抱歉，该订单还没有支付');}
if($l['state']==10){err('该订单已经完成');}
if($l['state']>10){err('抱歉，该订单为售后订单');}
$set=get_set(1);
order_finish($l);
err('ok');
break;


case 'order_finish'://订单完成
$user=is_login(1);
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('抱歉，请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
$l=db1($sql);
if(!$l){err('没有找到订单，或者您没有管理的权限');}
if($l['state']<1){err('抱歉，该订单还没有支付');}
if($l['state']==10){err('该订单已经完成');}
if($l['state']>10){err('抱歉，该订单为售后订单');}
$set=get_set(1);
$re=order_finish($l);
if($re){
$err['err_msg']=$re;
}
echo json_encode($err);
break;
case 'order_is_send'://完成配送
$user=is_login(1);
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('抱歉，请指定订单');}
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
$l=db1($sql);
if(!$l){err('没有找到订单，或者您没有管理的权限');}
if($l['state']<1){err('抱歉，该订单还没有支付');}
if($l['state']>5){err('抱歉。该订单已经完成配送');}
$arr['state']=5;

update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');

$temp=get_my_temp('订单配送通知',$hd_id);
//配送通知推送
if($temp){$shop=get_hd_shop($l['shop_id']);
$data['template_id']=$temp['temp_id'];
$data['page']='/pages/order/index';
$data['openid']=$user['openid'];
$data['data']=array(
'character_string1'=>array('value'=>$l['id']),
'phrase10'=>array('value'=>'配送成功'),
'time9'=>array('value'=>date('Y-m-d H:i:s',time())),
'phone_number8'=>array('value'=>$shop['phone']),
'thing6'=>array('value'=>'请尽快到取货地点进行取货。'),
);

$re=min_msg($data);

if($re['errmsg']=='ok'){
$arrs['is_send']=1;
update($cfg['e'].'temp_msg',$arrs,' where id="'.$temp['id'].'"');
}
}

err('ok');
break;


case 'finish_the_order'://订单完成
$user=is_login(1);if(!$user){err('no_login');}
if(!$id){err('请指定订单');}
$c=g('c');
$sql='select * from '.$cfg['e'].'order where id="'.$id.'" and state in (80,88)';
if($c=='adm'){
$shop=get_shop();
$sql.=' and shop_id="'.$shop['id'].'"';
}else{
$sql.=' and user_id="'.$user['id'].'"';
}
$l=db1($sql);
if(!$l){err('没有找到订单，或者您没有管理的权限');}
$arr['state']=10;
update($cfg['e'].'order',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'order_pro_state'://订单商品配送状态
$user=is_login();
if(!$user){err('no_login');}
if(!$id){err('请指定商品');}
$shop=get_shop();

$sql='select * from '.$cfg['e'].'order_pro where id="'.$id.'" and shop_id="'.$shop['id'].'"';
$l=db1($sql);
if(!$l){err('没有找到订单商品，或您没有编辑的权限');}
$arr['state']=2;
update($cfg['e'].'order_pro',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'get_my':
$user=is_login();
if(!$user){err('no_login');}
$err['user']=$user;
$shop=get_shop();
$err['shop']=$shop;
$err['set']=$set;

$help[1]='您可以将余额提现到您零钱。';
$help[2]='暂不开通银行卡提现功能。';

$err['help']=$help;
echo json_encode($err);

break;


case 'money_pay'://支付到零钱
$user=is_login(1);
if(!$user){err('no_login');}
$shop=get_shop();
$money=$shop['balance'];
if($money<=0){err('抱歉，您没有金额可以提现');}
$arr['state']=0;
$arr['user_id']=$user['id'];
$arr['money']=$money;
$arr['trade_no']=time().$user['id'];
$arr['date']=time();
$arr['c']=g('c');
$id=insert($cfg['e'].'money_pay',$arr);
$set=get_set(1);
if($id){
if($arr['c']=='user'){//零钱
$cc='零钱';
$re=money_pay_user($money,$arr['trade_no'],'提现到零钱',1);
}else{//银行卡
$cc='银行卡';
$re=money_pay_bank($money,$arr['trade_no'],$bank,'提现到银行卡',1);
}
$arrs['payment_no']=$re['payment_no'];
$arrs['payment_time']=$re['payment_time'];
$arrs['state']=1;
update($cfg['e'].'money_pay',$arrs,' where id="'.$id.'"');//更新

//账单更新
$zd['date']=time();
$zd['order_id']=$arr['trade_no'];
$zd['shop_id']=$shop['id'];
$zd['title']='提现到'.$cc;
$zd['user_id']=$user['id'];
$zd['c']='提现';
$zd['money']=-$money;
$zd['nickname']=base64_encode($user['nickname']);
$zd['logo']=$user['logo'];
insert($cfg['e'].'pay',$zd);

zd_balance($shop);//账单结算

err('ok');
}
break;

case 'shop_state'://门店状态
$user=is_login();
if(!$user){err('no_login');}
if(!$user['shop_id']){err('抱歉，您还没有创建门店');}
$val=g('val');
if($val!='stop'){$arr['shop_state']=1;$arrs['state']=1;
$text='您的门店上线成功';
}else{$arr['shop_state']=0;$text='您的门店停用成功';$arrs['state']=0;}

update($cfg['e'].'users',$arr,' where id="'.$user['id'].'"');
update($cfg['e'].'shop_gz',$arrs,' where shop_id="'.$user['shop_id'].'"');
sql_error();

$err['err_msg']=$text;
echo json_encode($err);
break;

case 'get_adm_msg':
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
$sql='select * from '.$cfg['e'].'msg where shop_id="'.$shop['id'].'" ';
$state=g('state');
if($state==99 or $state==1){
$sql.=' and state="'.$state.'" ';
}else{
$sql.=' and state=0 ';
}

$page=g('page');if(!$page){$page=1;}
$page_num=20;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$v['hd_title']=base64($v['hd_title']);
$v['nickname']=base64($v['nickname']);
$v['msg']=base64($v['msg']);
$v['re']=base64($v['re']);
$v['date']=date('Y-m-d H:i:s',$v['date']);
$list['item_'.$v['id']]=$v;

}
$err['list']=$list;
}
$err['set']=$set;
echo json_encode($err);
break;


case 'check_msg'://审核消息
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定消息');}
$arr['state']=g('state');if($arr['state']!=1){$arr['state']=99;}
$arr['re']=g('re');
if($arr['re']){$arr['re']=base64_encode($arr['re']);}
$arr['re_date']=time();
$sql='select * from '.$cfg['e'].'msg where shop_id="'.$shop['id'].'" and id="'.$id.'"';
$l=db1($sql);
if(!$l){err('没有找到评论，或者您没有查看的权限');}
update($cfg['e'].'msg',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'hb_user_get'://红包用户获取
$user=is_login();if(!$user){exit;}//没有登录 不操作
$from_id=g('from_id');if(!$id){exit;}
if(!$from_id or !is_numeric($from_id)){exit;}//没有邀请信息 
$sql='select * from '.$cfg['e'].'users where id="'.$from_id.'" ';
$u=db1($sql);if(!$u){return;}
if($redis){
$is_get=$redis->get('hd_'.$id.'_u_'.$user['id']);
if($is_get){exit;}
$hb_num=$redis->get('hd_'.$id.'_get_'.$u['id']);
if(!$hb_num){$hb_num=0;}
}else{
$sql='select id from '.$cfg['e'].'hb_user where user_id="'.$u['id'].'" and u_id="'.$user['id'].'" and hd_id="'.$id.'"';
$is_get=db1($sql);
if($is_get){exit;}//已经获取
}

$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and state=1 ';
$hd=db1($sql);if(!$hd){exit;}
if($hd['end']<time()){exit;}
if($hd['start']>time()){exit;}
$time=$hd['end']-$hd['start'];
if($hd['hb_price'] and $hd['hb_num'] and $hd['hb_total']){}else{exit;}
$num=$hd['hb_num']-$hd['hb_is_get'];
if($num<=0){exit;}

$arr['hd_id']=$hd['id'];
$arr['user_id']=$u['id'];
$arr['date']=time();
$arr['u_id']=$user['id'];
$arr['nickname']=base64_encode($user['nickname']);
$arr['logo']=$user['logo'];

$idxx=insert($cfg['e'].'hb_user',$arr);

if($idxx){
$hb_num++;
if($redis){
r_set('hd_'.$id.'_u_'.$user['id'],$idxx,$time);
r_set('hd_'.$id.'_get_'.$u['id'],$hb_num,$time);
}
}

err('ok');
break;

case 'hb_get'://获取红包
$user=is_login(1);
if(!$user){err('no_login');}
if(!$id){err('没有指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" ';
$l=db1($sql);
if(!$l){err('抱歉，没有找到活动内容');}
if($l['state']!=1){err('抱歉，该活动还没有上线');}
if($l['start']>time()){err('抱歉，该活动还没有开始');}
if($l['end']<time()){err('抱歉，活动已经结束了。');}
if($l['hb_total']==0){err('抱歉，红包已经抢光了，下次抓紧了');}
if($l['hb_price'] and $l['hb_num']){}else{err('抱歉，该活动没有启动现金红包功能');}
$total=number_format($l['hb_price']*$l['hb_total'],2,'.','');
//获取的数据
$sql='select count(id) as num from '.$cfg['e'].'hb_user where user_id="'.$user['id'].'" and hd_id="'.$l['id'].'" ';
$u=db1($sql);
if(!$u){err('您还没有邀请朋友参与，邀请'.$l['hb_num'].'位朋友瓜分￥'.$total.'元现金红包');}
$num=$u['num'];
if($num<$l['hb_num']){
err('您已经邀请了'.$num.'位朋友，再邀请'.($l['hb_num']-$num).'位朋友就可以瓜分￥'.$total.'元现金红包');
}
//发送现金红包
$arr['state']=0;
$arr['user_id']=$user['id'];
$arr['money']=$l['hb_price'];
$arr['trade_no']=time().$user['id'];
$arr['date']=time();
$arr['nickname']=base64($user['nickname'],1);
$arr['logo']=$user['logo'];
$arr['hd_id']=$l['id'];
$arr['shop_id']=$l['shop_id'];
$arr['u_id']=$l['user_id'];
$id=insert($cfg['e'].'hb_pay',$arr);

if($id){
$re=money_pay_user($arr['money'],$arr['trade_no'],'提现到零钱',1);//支付到零钱
$arrs['payment_no']=$re['payment_no'];
$arrs['payment_time']=$re['payment_time'];
$arrs['state']=1;
update($cfg['e'].'hb_pay',$arrs,' where id="'.$id.'"');//更新

//账单更新
$zd['date']=time();
$zd['order_id']=$arr['trade_no'];
$zd['shop_id']=$l['shop_id'];
$zd['title']='获取现金红包';
$zd['user_id']=$user['id'];
$zd['c']='现金红包';
$zd['money']=-$arr['money'];
$zd['nickname']=base64_encode($user['nickname']);
$zd['logo']=$user['logo'];
insert($cfg['e'].'pay',$zd);

zd_balance($shop);//账单结算
//冻结资金减少
$sql='update '.$cfg['e'].'shop set freeze=freeze-'.$arr['money'].' where id="'.$l['shop_id'].'"';
sql_query($sql);
err('您的现金红包￥'.$arr['money'].'元获取成功，请查询您的微信钱包。');
}
break;

case 'user_save':
$user=is_login();
if(!$user){err('no_login');}
$data=p('data');if(!$data){exit;}
$data=json_decode($data,1);if(!$data){exit;}
$c=['logo','nickname','phone','pro','city','dist','addr','latitude','longitude','name'];
foreach($c as $v){
if($data[$v]){
if($data[$v]=='null'){$data[$v]='';}    
$arr[$v]=$data[$v];}
}

if(!$arr){exit;}
if($arr['nickname']){$arr['wx_nickname']=$arr['nickname'];$arr['nickname']=base64_encode($arr['nickname']);}
update($cfg['e'].'users',$arr,' where id="'.$user['id'].'"');
r_set($cfg['path'].'user_'.$user['id'],'',600);
err('ok');

break;



case 'get_phone'://获取手机号码
$user=is_login();if(!$user){err('no_login');}
$code=g('code');
if(!$code){err('获取手机号码错误');}

$token=getAccessToken ($cfg['wx_xcx_appid'], $cfg['wx_xcx_secret']);
$url='https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$token;
$data['code']=$code;
$re = post( $url, json_encode($data));
if($re['errmsg']=='ok'){
$phone=$re['phone_info']['phoneNumber'];
}else{err('ok');}


$err['phone']=$phone;
echo json_encode($err);
break;
case 'shop_qy'://签约
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
if(!$user['shop_id']){err('抱歉，您没有签约的权限');}
$arr['is_agree']=1;$shop['is_agree']=1;
update($cfg['e'].'shop',$arr,' where id="'.$shop['id'].'"');
$err['shop']=$shop;
echo json_encode($err);
break;


case 'temp_submit':
$user=is_login();if(!$user){err('ok');}
$data=p('data');if(!$data){err('ok');}
$data=json_decode($data,1);
$arr['hd_id']=g('id');$arr['order_id']=g('order_id');
$arr['date']=time();
$arr['logo']=$user['logo'];
$arr['nickname']=base64($user['nickname'],1);
$arr['user_id']=$user['id'];
foreach($data as $v){
$sql='select * from '.$cfg['e'].'temps where temp_id="'.$v.'" and c="user"';
$l=db1($sql);
$arr['c']=$l['s'];
$arr['title']=$l['title'];
$arr['temp_id']=$l['temp_id'];
insert($cfg['e'].'temp_msg',$arr);
}
err('ok');
break;



case 'get_jifen_list'://活动我的积分列表
$user=is_login();
if(!$user){err('no_login');}
$sql='select * from '.$cfg['e'].'jifen where user_id="'.$user['id'].'" ';
$page=g('page');if(!$page or !is_numeric($page)){$page=1;}
$page_num=30;$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$v['date']=date('Y-m-d H:i:s',$v['date']);
$list['item_'.$v['id']]=$v;
}
$err['lsit']=$list;
}
$err['set']=$set;
echo json_encode($err);
break;


case 'get_index':
$user=is_login();
if($user){$err['user']=$user;}
$shop_id=$set['shop_id'];
$err['set']=$set;
$shop=get_hd_shop($shop_id);
$err['shop']=$shop;
$sql='select * from '.$cfg['e'].'pro where state=1 ';

$sql.=' order by o desc,id desc limit 300';
$l=db($sql);


if($l){
foreach($l as $v){
if($v['is_new']){
$v['img']=get_img($v['img'],'_480');
}else{
$v['img']=get_img($v['img'],'_240');}
$v['title']=base64($v['title']);
if($v['info']){$v['info']=base64($v['info']);}
$v['p']=explode(".",$v['price']);
if($v['data']){$v['data']=unserialize(out($v['data']));}
if(!$v['c']){$v['c']='未分类';}
if($v['is_hot']){$v['c']='热销';}
$x[$v['c']][]=$v;
}

$temp=$x['未分类'];


$xx['热销']=$x['热销'];
unset($x['未分类'],$x['热销']);

if($x){
foreach($x as $k=>$v){
$xx[$k]=$v;
}
}


$xx['未分类']=$temp;

//进行数据处理
$height=0;$num=0;
foreach($xx as $k=>$t){
if(!$cc[$k]){
$cc[$k]['s']=$height;
if($xt){$cc[$xt]['e']=$height-1;}
$xt=$k;
}

$height+=35;

$cc[$k]['total']=count($t);
$cc[$k]['num']=0;$cc[$k]['load']=0;
foreach($t as $v){
if($v['is_new']){$height+=255;}else{$height+=105;}
$num++;
$cc[$k]['id']['item_'.$v['id']]=$v['id'];
$v['cc']=$k;
$list['item_'.$v['id']]=$v;

}

}

$cc[$xt]['e']=$height-1;

if($cc['热销']){
$cc['热销']['icon']='icon-hotfill';
$cc['热销']['bg']='crimson';
$cc['热销']['color']='crimson';
}

$err['list']=$list;
$err['c']=$cc;
}
echo json_encode($err);




break;

case 'get_shop_tz'://获取门店团长
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();

$sql='select * from '.$cfg['e'].'tz where shop_id="'.$shop['id'].'" ';
$key=g('key');
if($key){$sql.=' and nickname like "'.a($key).'" ';}


$page=g('page');if(!$page){$page=1;}$page_num=30;
$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$u=get_user($v['user_id']);
if($u){
$v['nickname']=$u['nickname'];
$v['logo']=$u['logo'];

$list['item_'.$v['id']]=$v;

}
}
$err['list']=$list;

}
$err['set']=$set;
$err['shop']=$shop;
echo  json_encode($err);

break;
















/////////////////send ////////////
case 'get_send_edit':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();$err['shop']=$shop;
$err['user']=$user;
$err['set']=$set;
if($id){
$sql='select * from '.$cfg['e'].'send where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){
    $sql.=' and user_id="'.$user['id'].'" ';
}
$l=db1($sql);
if($l){
$l['nickname']=base64($l['nickname']);
if($l['img']){$l['img']=unserialize(out($l['img']));}
$l['msg']=base64($l['msg']);
if($l['start']){
$l['start']=str_replace(date('Y年m月d日 '),'',$l['start']);
}

$err['list']=$l;
}
}

$err['c']=['同城配送','到点取货'];

$t=date('H');
$xx=date('i');

for($j=0;$j<=3;$j++){
$x=$j*15;$tt=$t.'时';
if($x>$xx){
$tt.=$x.'分';
$time[]=$tt;
}
}


for($i=$t+1;$i<=23;$i++){
for($j=0;$j<=3;$j++){
$x=$j*15;
$t=$i.'时';
if($x){$t.=$x.'分';}
$time[]=$t;
}
}
$err['time']=$time;


echo json_encode($err);
break;


case 'job_save':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['start','msg','re','img','price','guest_name','guest_phone','pro','city','dist','guest_addr','name','phone','addr','guest_lat','guest_lng','lat','lng','c'];
foreach($c as $v){
if($data[$v]){
if($data[$v]=='null'){$data[$v]='';}
$arr[$v]=$data[$v];
}
}

if(!$arr){err('保存数据错误');}
if($id){
$sql='select * from '.$cfg['e'].'send where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){
    $sql.=' and user_id="'.$user['id'].'" ';
}
$l=db1($sql);if(!$l){err('抱歉，没有找到您的任务，或者您没有编辑的权限');}
}

if($arr['img']=='null'){$arr['img']='';}
if($arr['img']){
$arr['img']=serialize($arr['img']);}
if($arr['msg']){$arr['msg']=base64($arr['msg'],1);}
if($arr['price']==null){$arr['price']=0;}
if($arr['start']){
$arr['start']=date('Y年m月d日 ').$arr['start'];
}


if($l){
update($cfg['e'].'send',$arr,' where id="'.$l['id'].'"');
$err['id']=$l['id'];
echo json_encode($err);exit;
}

if(!$arr['c']){err('请选择任务分类');}
if(!$arr['pro']){err('请选择任务内容');}
if(!$arr['msg']){err('请输入任务内容');}
//if(!$arr['guest_lat']){err('请选择送货地址');}
//if(!$arr['la'])
$arr['user_id']=$user['id'];
$arr['shop_id']=$shop['id'];
$arr['date']=time();
$arr['state']=0;
$arr['logo']=$user['logo'];
$arr['nickname']=base64($user['nickname'],1);
$id=insert($cfg['e'].'send',$arr);
$err['id']=$id;
echo json_encode($err);
break;


case 'job_publish':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定任务');}
$sql='select * from '.$cfg['e'].'send where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){
    $sql.=' and user_id="'.$user['id'].'" ';
}
$l=db1($sql);if(!$l){err('抱歉，没有找到您的任务，或者您没有操作的权限');}
if($l['state']>0){err('该任务已经发布');}
if(!$l['c']){err('请选择分类');}
if(!$l['msg']){err('请输入任务信息');}
if(!$l['start']){err('请选择配送的时间');}
if(!$l['pro']){err('地区没有选择');}
if(!$l['guest_lat']){err('请选择配送定位');}
if(!$l['lat']){err('请选择您的位置');}
if(!$l['guest_phone']){err('请输入顾客的联系电话');}
if(!$l['guest_addr']){err('请输入顾客地址');}
if(!$l['name']){err('请输入您的姓名或店名');}
if(!$l['phone']){err('请输入您的联系电话');}
if($l['price']<=0){err('请输入配送金额');}

//配送金额问题
if($shop['balance']<$l['price']){err('抱歉，您的资金余额不足，请充值后继续');}

$zd['shop_id']=$shop['id'];
$zd['date']=time();
$zd['send_id']=$l['id'];
$zd['nickname']=base64($user['nickname'],1);
$zd['logo']=$user['logo'];
//管理员账单
$zd['user_id']=$user['id'];
$zd['title']='配送费冻结';
$zd['c']='资金冻结';
$zd['money']=-$l['price'];//金额
insert($cfg['e'].'pay',$zd);
zd_balance($shop);

$sql='update '.$cfg['e'].'shop set freeze=freeze+'.$l['price'].' where id="'.$shop['id'].'"';
sql_query($sql);
$arr['state']=1;$arr['pub_date']=time();
update($cfg['e'].'send',$arr,' where id="'.$l['id'].'"');
err('ok');

break;


case 'job_stop':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定任务');}
$sql='select * from '.$cfg['e'].'send where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
if($user['is_emp']){
    $sql.=' and user_id="'.$user['id'].'" ';
}
$l=db1($sql);if(!$l){err('抱歉，没有找到您的任务，或者您没有操作的权限');}
if($l['state']>1){err('抱歉，您的任务已经配送，无法停用');}
if($l['sender_id']>0){err('抱歉，您的任务已经接单，如果强制停止，您将支付部分配送费用给配送员');}
$arr['state']=0;
update($cfg['e'].'send',$arr,' where id="'.$l['id'].'"');
err('ok');

break;



case 'get_send_list':
$user=is_login();
$err['user']=$user;
$err['set']=$set;
$sql='select * from '.$cfg['e'].'send where state in (1,2) ';
$key=g('key');
if($key){$sql.=' and msg like "%'.$key.'%" ';}
$page=g('page');if(!$page){$page=1;}$page_num=20;
$start=($page-1)*$page_num;
$sql.=' order by id desc limit '.$start.','.$page_num;
$l=db($sql);
if($l){
foreach($l as $v){
$v['msg']=base64($v['msg']);
if($v['img']){$v['img']=unserialize(out($v['img']));
$v['img_num']=count($v['img']);
}
$v['nickname']=base64($v['nickname']);
$v['up_time']=date_ff($v['pub_date']);

if($l['state']==1){
$v['time']='接单中';
}else{
$v['time']='配送中';
}


$list['item_'.$v['id']]=$v;
}
$err['list']=$list;
}
echo json_encode($err);
break;







case 'order_analyze'://活动订单统计
$user=is_login();
if(!$user){err('no_login');}
$shop=get_shop();
if(!$id){err('请指定活动');}
$sql='select * from '.$cfg['e'].'hd where id="'.$id.'" and shop_id="'.$shop['id'].'" ';
$l=db1($sql);
if(!$l){err('抱歉，没有找到活动，或者您没有查看的权限');}

$sql='select * from '.$cfg['e'].'order where hd_id="'.$l['id'].'" ';

$order=db($sql);
if($order){
foreach($order as $v){
$pros=get_order_pro($v['id']);
if($pros){
foreach($pros as $t){
switch($t['c']){
default:
if(!$pro[$v['state']][$t['pro_id']]){$pro[$v['state']][$t['pro_id']]['title']=$t['title'];
 $pro[$v['state']][$t['pro_id']]['img']=$t['img'];}
$pro[$v['state']][$t['pro_id']]['num']+=$t['num'];       
break;
case '秒杀':
if(!$miao[$v['state']][$t['pro_id']]){$miao[$v['state']][$t['pro_id']]['title']=$t['title'];
$miao[$v['state']][$t['pro_id']]['img']=$t['img'];}
$miao[$v['state']][$t['pro_id']]['num']++;     
break;
case '赠品':
if(!$zp[$v['state']][$t['pro_id']]){$zp[$v['state']][$t['pro_id']]['title']=$t['title'];
$zp[$v['state']][$t['pro_id']]['img']=$t['img'];}
$zp[$v['state']][$t['pro_id']]['num']++;     
break;
}  

}
}

$oo[$v['state']]++;//订单数
$price[$v['state']]+=$v['total'];

}

}
$l['title']=base64($l['title']);
$err['miao']=$miao;
$err['list']=$l;
$err['pro']=$pro;
$err['mz']=$zp;
$err['state']=$oo;
$err['price']=$price;
$err['set']=$set;
echo json_encode($err);
break;


case 'get_emoji':
$sql='select emoji from '.$cfg['e'].'emoji order by o desc,id asc';
$l=db($sql);
$err['list']=$l;
echo json_encode($err);
break;



case 'tz_band'://绑定团长
$user=is_login();if(!$user){err('no_login');}
if(!$id){err('请指定门店');}
$shop=get_hd_shop($id);
if(!$shop){err('没有找到门店');}
$sql='select * from '.$cfg['e'].'tz where shop_id="'.$shop['id'].'" and user_id="'.$user['id'].'"';
$l=db1($sql);
if($l){err('您已经绑定了该门店');}
$arr['date']=time();
$arr['shop_id']=$shop['id'];
$arr['user_id']=$user['id'];
$arr['state']=1;
$arr['people_get']=0;$arr['nickname']=$user['nickname'];
$id=insert($cfg['e'].'tz',$arr);
err('ok');
break;

case 'tz_unbind':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
if(!$user['shop_id']){err('仅管理员才能进行操作');}
if(!$id){err('请指定团长');}
$sql='delete from '.$cfg['e'].'tz where id="'.$id.'" and shop_id="'.$shop['id'].'"';
sql_query($sql);

err('ok');
break;

case 'tz_state':
$user=is_login();if(!$user){err('no_login');}
$shop=get_shop();
if(!$user['shop_id']){err('仅管理员才能进行操作');}
if(!$id){err('请指定团长');}
$arr['state']=g('val');if($arr['state']!=1){$arr['state']=0;}
update($cfg['e'].'tz',$arr,' where id="'.$id.'" and shop_id="'.$shop['id'].'"');
err('ok');
break;

case 'shop_info_save'://门店信息的保存
$user=is_login();if(!$user){err('no_login');}
$shop=get_my_shop();
if(!$shop){err('您的门店信息有误');}
$data=p('data');
if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$c=['shop_name','shot_name','pro','city','dist','addr','phone','latitude','longitude','logo','name','print_data','yun_title','bg'];
$n=['pay_end','yun_id'];
foreach($c as $v){
if($data[$v]){$arr[$v]=$data[$v];$can++;}
}

foreach($n as $v){
if(is_numeric($data[$v])){$arr[$v]=$data[$v];$can++;}
}

if(!$can){err('ok');}
if($arr['shop_name']){$arr['shop_name']=base64($arr['shop_name'],1);}
if($arr['print_data']){$arr['print_data']=serialize($arr['print_data']);}
update($cfg['e'].'shop',$arr,' where id="'.$shop['id'].'"');
err('ok');
break;

case 'my_shop_state'://修改状态
$user=is_login();if(!$user){err('no_login');}
$shop=get_my_shop();
if(!$shop){err('您的门店信息有误');}
$data=p('data');
if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
if($data['state']==1){//开店

if(!$shop['shop_name']){err('请输入门店名称');}
if(!$shop['pro']){err('请选择省市区');}
if(!$shop['name']){err('请输入联系人');}
if(!$shop['phone']){err('请输入联系号码');}
$arr['state']=1;
}else{//关闭
$arr['state']=0;
}

update($cfg['e'].'shop',$arr,' where id="'.$shop['id'].'"');
r_set('my_shop_'.$user['id'],'',300);
err('ok');

break;

case 'print'://打印订单
    $user=is_login();if(!$user){ret('no_login');}
    $order_id=g('order_id');if(!$order_id){err('请选择订单');}
    err( printer($order_id));
    break;
case 'tianjia'://添加商户
    err(false);
    break;
case 'tousu'://添加商户
    err(true);
    break;
case 'ceshi'://添加商户
    $sql='select * from '.$cfg['e'].'order where id="60" ';
    $order=db1($sql);
    err(upload_shipping_info($order,'4200002684202507132061630815'));
    break;

}

?>